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();
Answer from M.Bugiel on Stack OverflowGood 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.
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
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);
}
}
arrays - How do I find a String list's length in Java? - Stack Overflow
terminology - What term describes a list of exactly length 1? - Software Engineering Stack Exchange
[Java] size vs. length
Was there a point of time when array.length is a O(N) operation in Java
How does the size() method work in Java Lists?
Why is the time complexity of the size() method constant?
Why is the size of a List different from its capacity?
Videos
Do not come up with a new word. Your name is perfectly fine: It is unambiguous and specific and consequently leaves no doubt to the reader what you are talking about.
By contrast, singleton, unary, 1-tuple or any other term borrowed from mathematics or software engineering carries with it a baggage of preconceptions which are confusing. A list with a single element in it is emphatically not a singleton. It has nothing to do with unary operators, and a list is clearly not a tuple in C++. It is a list with one element in it, not more, not less.
That is sometimes a perceived downside of a simple approach in programming: It seems unsophisticated, and hence of a lesser value. Look at Duff's device! Marvel at the ingenuity of boost.lambda! But then listen to Jim Radigan, who leads the VC++ compiler team:
One of the other things that happen when we go to check code into the compiler is we do peer code review. So if you survive that, it’s probably ok, it’s not too complex. But if you try to check in meta-programming constructs with 4-5 different include files and virtual methods that wind up taking you places you can’t see unless you’re in a debugger – no one is going to let you check that in.
That your peer is able to understand your code right away because it is plain and simple and calls things what they are is not a sign that you didn't realize your full programming potential. It is a sign of excellence. Do not look for a Latin word.
There is as far as I know, no widely accepted term in SW engineering to described a list with exactly one element
In mathematics, and more precisely in the set theory, a set with only one element is called a singleton. Unfortunately, in SW engineering, the term is so heavily associated with a design pattern, that using it for another purpose might create a confusion.
Anyway, a list is ordered, so a set is not a list. An ordered list of items is called a sequence in mathematics. And a sequence with exactly one element is called a 1-tuple. Unfortunately, a tuple corresponds to specific data structures, so this term, even converted to letters-only, would also be very ambiguous.
Finally, if mathematics don’t help, let’s look at literature! The contrary of plural is singular. According to the Meriam-Webster dictionary, it means “of, or relating to, one person, thing or instance”. So a singular list should express exactly what you want. This term is even reinforced by its ambiguity: “singular” is associated in other contexts with strangeness, and a list with only one element is indeed a strange list (as πάντα ῥεῖ already pointed out in the comments. But I’d nevertheless prefer to explain it a comment at first use ;-)