cbInfo009
cbInfo009

Reputation: 71

Java get element from list of list

I have a list like this :

List<List<String>> myList = new ArrayList<List<String>>();

that I fill with other lists :

List<String> lst = new ArrayList<String>();
String val1;
String val2; 
val1 = "java";
val2 = "c++";
lst.add(val1);
lst.add(val2);
myList.add(lst);

List<String> lst2 = new ArrayList<String>();
val1 = "pizza";
val2 = "fruit";
lst.add(val1);
lst.add(val2);
myList.add(lst);

I want to get the elements of the list by giving index something like :

String valeur = myList[0].val1 ;
String valeur2 = myList[1].val2 ;

which of course is a wrong way; I don't know how to do this; I read something about closures but I don't know what is it .. any help will be apprecdiated , and I hope my question is clear don't hesitate to correct or ask me if something is wrong.

Thank you.

Upvotes: 0

Views: 12707

Answers (2)

Sanskruti Nashikkar
Sanskruti Nashikkar

Reputation: 1

try this String valeur = myList.get(0).get(0)

Upvotes: 0

Yassin Hajaj
Yassin Hajaj

Reputation: 22005

Use List#get()

Returns the element at the specified position in this list.

String valeur = myList.get(index).get(index);

Upvotes: 4

Related Questions