Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add one arraylist to other arraylist in android?

Tags:

android

I have created 3 arraylists and stored data in it. I need to pass one single arraydata to other page and so I have added my individual arrays to the main array. But when I added one array to other the data is not being aded to that array. Here is my code:

static ArrayList<ArrayList<String>> stringList1=new ArrayList<ArrayList<String>>();
static ArrayList<ArrayList<String>> stringList3; 

stringList1 = Mypage.stringList1;

 ArrayList<String> optionlist = new ArrayList<String>();
stringList3 = new ArrayList<ArrayList<String>>();stringList3 = dbAdapter.selectRecordsFromDBList(query2, null); 
 for (int i = 0; i < stringList3.size(); i++) {
    optionlist = stringList3.get(i);
    System.out.print("option list size");
    System.out.print(optionlist.size());
    stringList1.add(optionlist);
    System.out.println("total stringlist1"+stringList1.get(0));

I am getting the stringList1 array values from Mypage and accessing that in new page. In the new page I am trying to add the optionlist array to stringList1 by giving stringList1.add(optionlist) but the data is not adding. Where I went wrong? Please help me regarding this... Thanks in Advance

like image 809
RaagaSudha Avatar asked Dec 29 '11 10:12

RaagaSudha


People also ask

How do you add an ArrayList to another ArrayList?

Approach: ArrayLists can be joined in Java with the help of Collection. addAll() method. This method is called by the destination ArrayList and the other ArrayList is passed as the parameter to this method. This method appends the second ArrayList to the end of the first ArrayList.

How do I copy one ArrayList to another in Android?

In order to copy elements of ArrayList to another ArrayList, we use the Collections. copy() method. It is used to copy all elements of a collection into another.

How do you add in between ArrayList?

Elements can be added in the middle of an ArrayList by using the java. util. ArrayList. add() method.

How do you copy a list into an ArrayList?

ArrayList clone() method in Java with Examples clone() method is used to create a shallow copy of the mentioned array list. It just creates a copy of the list. Parameters: This method does not take any parameters. Return Value: This function returns a copy of the instance of Linked list.


2 Answers

Use addAll() It appends all of the elements in the specified ArrayList to the end of this list, in the order that they are returned by the specified Collection's Iterator.

use stringList1.addAll(optionlist); instead of stringList1.add(optionlist);

like image 117
Sunil Kumar Sahoo Avatar answered Oct 04 '22 00:10

Sunil Kumar Sahoo


After seeing your code I can guess that you have problem that elements of arrayList is not copy in another arrayList.If this is your issue then change your code like below

static ArrayList<ArrayList<String>> stringList1=new ArrayList<ArrayList<String>>();
static ArrayList<ArrayList<String>> stringList3; 

stringList1 = Mypage.stringList1;

ArrayList<String> optionlist;
stringList3 = new ArrayList<ArrayList<String>>();
stringList3 = dbAdapter.selectRecordsFromDBList(query2, null); 

 for (int i = 0; i < stringList3.size(); i++) {
    optionlist = new ArrayList<String>();
    optionlist = stringList3.get(i);
    System.out.print("option list size");
    System.out.print(optionlist.size());
    stringList1.add(optionlist);
    System.out.println("total stringlist1"+stringList1.get(0));
 }
like image 44
Dharmendra Avatar answered Oct 04 '22 01:10

Dharmendra