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
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.
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.
Elements can be added in the middle of an ArrayList by using the java. util. ArrayList. add() method.
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.
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);
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));
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With