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);
}
}
java - Initial size for the ArrayList - Stack Overflow
How to get array list size in java jsp?
What is the maximum size of a bitset in Java?
[JAVA] Why is my ArrayList size always 0
How does the size() method work in Java Lists?
Why is the size of a List different from its capacity?
How often should I call the size() method?
Videos
You're confusing the size of the array list with its capacity:
- the size is the number of elements in the list;
- the capacity is how many elements the list can potentially accommodate without reallocating its internal structures.
When you call new ArrayList<Integer>(10), you are setting the list's initial capacity, not its size. In other words, when constructed in this manner, the array list starts its life empty.
One way to add ten elements to the array list is by using a loop:
for (int i = 0; i < 10; i++) {
arr.add(0);
}
Having done this, you can now modify elements at indices 0..9.
If you want a list with a predefined size you can also use:
List<Integer> arr = Arrays.asList(new Integer[10]);
Use arrayListName.size() for ArrayLists.
Have a look at http://docs.oracle.com/javase/6/docs/api/java/util/ArrayList.html
You can use size() method to find size of the ArrayList.But I strongly recommend you to use isEmpty() method to check whether list is empty or not (instead of list.size()==0 checking).
ArrayList#isEmpty
Returns true if this list contains no elements.