1
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 ?

6
  • 2
    mainlist.add(maintext) --> you are adding String in place of arrayList. Commented 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 ) Commented Aug 17, 2012 at 6:32
  • 2
    Are you sure that this line mainlist.add(maintext); is OK? I would say mainlist.add(childlist). Commented 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 times Commented Aug 17, 2012 at 6:32
  • 1
    You 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. Commented Aug 17, 2012 at 6:33

1 Answer 1

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.
Sign up to request clarification or add additional context in comments.

1 Comment

i tried for(i=0;i<3;i++){ mainlist.get(i).get(2); } it will display mainlist 1 chlidlist 3rd item. 3x times

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.