By Alvin Alexander. Last updated: November 24, 2022
If you want to create and populate a Java ArrayList
with Java 9, 11, and newer, you can use this syntax:
List<> ints = ArrayList<Integer>(List.of(1,2,3));
As shown, this uses the usual ArrayList
constructor and the Java List.of
method. Once you have an ArrayList
like this you can continue to add new elements to it as usual:
ints.add(4);