Use
List<List<Integer>> list = new ArrayList<List<Integer>>();
or since Java 1.7
List<List<Integer>> list = new ArrayList<>();
Answer from Reimeus on Stack OverflowIt seems a lot easier from the very little time I spent on Python before making the jump to Java. I'm taking a college night class on Java, and lists are one thing that are confusing me. How do they work in Java?
Use
List<List<Integer>> list = new ArrayList<List<Integer>>();
or since Java 1.7
List<List<Integer>> list = new ArrayList<>();
You can define it as List<List<Integer>> list = new ArrayList<List<Integer>>();.
Then while defining the inner List you can take care of initialising it as ArrayList<Integer>.
java - Create a List of primitive int? - Stack Overflow
Java integer list - Stack Overflow
[java] working with an ArrayList of ArrayList <Integer>
How do I make integer lists in Java?
In Java the type of any variable is either a primitive type or a reference type. Generic type arguments must be reference types. Since primitives do not extend Object they cannot be used as generic type arguments for a parametrized type.
Instead use the Integer class which is a wrapper for int:
List<Integer> list = new ArrayList<Integer>();
If your using Java 7 you can simplify this declaration using the diamond operator:
List<Integer> list = new ArrayList<>();
With autoboxing in Java the primitive type int will become an Integer when necessary.
Autoboxing is the automatic conversion that the Java compiler makes between the primitive types and their corresponding object wrapper classes.
So the following is valid:
int myInt = 1;
List<Integer> list = new ArrayList<Integer>();
list.add(myInt);
System.out.println(list.get(0)); //prints 1
No there isn't any collection that can contain primitive types when Java Collection Framework is being used.
However, there are other java collections which support primitive types, such as: Trove, Colt, Fastutil, Guava
An example of how an arraylist with ints would be when Trove Library used is the following:
TIntArrayList list= new TIntArrayList();
The performance of this list, when compared with the ArrayList of Integers from Java Collections is much better as the autoboxing/unboxing to the corresponding Integer Wrapper Class is not needed.
If you want to rewrite a line on console, print a control character \r (carriage return).
List<Integer> myCoords = new ArrayList<Integer>();
myCoords.add(10);
myCoords.add(20);
myCoords.add(30);
myCoords.add(40);
myCoords.add(50);
Iterator<Integer> myListIterator = myCoords.iterator();
while (myListIterator.hasNext()) {
Integer coord = myListIterator.next();
System.out.print("\r");
System.out.print(coord);
Thread.sleep(2000);
}
code that works, but output is:
10
20
30
40
50
so:
List<Integer> myCoords = new ArrayList<Integer>();
myCoords.add(10);
myCoords.add(20);
myCoords.add(30);
myCoords.add(40);
myCoords.add(50);
for (Integer number : myCoords) {
System.out.println(number);
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}