ArrayList<ArrayList<String>> mainlist = new ArrayList<ArrayList<String>>();
ArrayList<String> childlist = new ArrayList<String>();
for (int i = 0; i < 3 i++){
String chlidtext = chlidlist + String.valueof(i);
String maintext = mainlist +String.Valueof(i);
childlist.add(chlidtext);
mainlist.add(maintext);
}
return mainlist;
i dont have an idea how to get 3rd element in every chlidlist ?
asked Aug 17, 2012 at 6:28
Bucks
6873 gold badges11 silver badges28 bronze badges
-
2mainlist.add(maintext) --> you are adding String in place of arrayList.Sandeep Pathak– Sandeep Pathak2012年08月17日 06:31:34 +00:00Commented Aug 17, 2012 at 6:31
-
You are having only 3 child nodes? (start the i value from 2 and then increment it by 3 )G_S– G_S2012年08月17日 06:32:12 +00:00Commented Aug 17, 2012 at 6:32
-
2Are you sure that this line mainlist.add(maintext); is OK? I would say mainlist.add(childlist).besworland– besworland2012年08月17日 06:32:24 +00:00Commented Aug 17, 2012 at 6:32
-
its not working if i use for/for each loop it display one 1st mainlist 3rd childlist item 3x timesBucks– Bucks2012年08月17日 06:32:48 +00:00Commented Aug 17, 2012 at 6:32
-
1You may get exception because you should add ArrayList<String> to mainlist, but you are adding String here. And you can get 3rd element of an arraylist by giving its index to get method. mainlist.get(2) will return 3rd element.android– android2012年08月17日 06:33:35 +00:00Commented Aug 17, 2012 at 6:33
1 Answer 1
You can access 3rd element of childList by:
mainList.get(index).get(2); // Here 1st get() will get the 1st element of mainList which conatins 1st child element and 2nd get() will get the 3rd element of childlist.
answered Aug 17, 2012 at 6:34
Shrikant Ballal
7,0977 gold badges43 silver badges61 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
Bucks
i tried for(i=0;i<3;i++){ mainlist.get(i).get(2); } it will display mainlist 1 chlidlist 3rd item. 3x times
default