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
-
No iteration required. Read this. stackoverflow.com/a/8666707/2707485Ashokchakravarthi Nagarajan– Ashokchakravarthi Nagarajan2015年07月01日 17:14:22 +00:00Commented Jul 1, 2015 at 17:14
3 Answers 3
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));
}
The problem seems that you are initializing your
ArrayList<String> optionlist;
only once that is outside the loop. So, in that case only the first ArrayList
gets stored. So, initialized the ArrayList<String> optionlist
inside the loop.
ArrayList<String> optionlist;
for (int i = 0; i < stringList3.size(); i++) {
optionlist = new ArrayList<String>();
optionlist = stringList3.get(i);
stringList1.add(optionlist);
}
-
Hi thanks for the response..I took a list but even then the same problem..plz help meRaagaSudha– RaagaSudha2011年12月29日 10:44:18 +00:00Commented Dec 29, 2011 at 10:44
-
I can't get you what you are trying to do actually, so please can you explain more.Lalit Poptani– Lalit Poptani2011年12月29日 10:47:39 +00:00Commented Dec 29, 2011 at 10:47
-
I am getting values from stringlist1 from page1 to page2 using stringlist1 = page1.stringlist1. Now I need to add my optionlist array which is in page2 to stringList1 arraylist. But the data is not adding to stringlist1.What is the mistake I am doing?RaagaSudha– RaagaSudha2011年12月29日 10:52:41 +00:00Commented Dec 29, 2011 at 10:52
-
hmmm the problem is you are only initializing your ArrayList only once outside the loop check my edited answer.Lalit Poptani– Lalit Poptani2011年12月29日 11:02:09 +00:00Commented Dec 29, 2011 at 11:02