Just use
int listCount = data.size();
That tells you how many lists there are (assuming none are null). If you want to find out how many strings there are, you'll need to iterate:
int total = 0;
for (List<String> sublist : data) {
// TODO: Null checking
total += sublist.size();
}
// total is now the total number of strings
Answer from Jon Skeet on Stack OverflowJust use
int listCount = data.size();
That tells you how many lists there are (assuming none are null). If you want to find out how many strings there are, you'll need to iterate:
int total = 0;
for (List<String> sublist : data) {
// TODO: Null checking
total += sublist.size();
}
// total is now the total number of strings
Java 8
import java.util.Arrays;
import java.util.List;
import java.util.ArrayList;
public class HelloWorld{
public static void main(String []args){
List<List<String>> stringListList = new ArrayList<>();
stringListList.add(Arrays.asList(new String[] {"(0,0)", "(0,1)"} ));
stringListList.add(Arrays.asList(new String[] {"(1,0)", "(1,1)", "(1,2)"} ));
stringListList.add(Arrays.asList(new String[] {"(2,0)", "(2,1)"} ));
int count=stringListList.stream().mapToInt(i -> i.size()).sum();
System.out.println("stringListList count: "+count);
}
}
Videos
How does the size() method work in Java Lists?
Why is the time complexity of the size() method constant?
How often should I call the size() method?
Good place to look for useful methods associated with List is documentation: https://docs.oracle.com/javase/8/docs/api/java/util/List.html
' size() Returns the number of elements in this list.'
List myList = Arrays.asList(1,2,3,4,5);
myList.size();
Presuming that you have a List, with several arrays inside the list, I would cast the object when you get the array to an array then call .length like;
String[] stuff1 = new String[]{"1", "1", "1"};
String[] stuff2 = new String[]{"2", "2", "2"};
String[] stuff3 = new String[]{"3","3","3"};
List list = new ArrayList();
list.add(stuff1);
list.add(stuff2);
list.add(stuff3);
String[] find = (String[])list.get(1);
int length = find.length;
But you really should do some googling before asking such a fundemental question.