简体   繁体   中英

get values in List of Lists Java

I have a List of List array

List <List<String>> fname;

System.out.println(fname.get(0).get(0));-->gives 1st file name

I want to retrieve each element in the array List without using two for loop (as a solution I thought of) because it will increase the complexity any help??

在Java 8中,您可以只使用一个语句:

fname.forEach(sublist -> sublist.forEach(element -> System.out.println(element)));

Note that while this is one loop, it is much easier to do it with nested for loops

List<List<String>> list;
int x = 0;
int y = 0;
while(x < list.size())
{
    if(y < list.get(x).size())
    {
        //do stuff with list.get(x).get(y)
        y++;
    }
    else
    {
        x++;
        y = 0;
    }
}

A much more preferred way to loop through all of the elements, and should be just as fast if not faster

List<List<String>> list;
for(List<String> l : list)
    for(String s : l)
        //do stuff with s

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM