Quick Index
Examples


//Constructing simple empty list
List<String> names = new ArrayList<>();

//Constructing initially empty list of Country objects
List<Country> countries = new ArrayList<>();

//Constructing simple (given) list
String[] strings = {"one", "two", "three", "four"};
List<String> list = new ArrayList<>(Arrays.asList(strings));

//One-Liner for simple (given) list
List<String> colorNames = new ArrayList<>(Arrays.asList("Yellow", "Cyan", "Magenta", "Green"));

//Print a list on one line
println("Colors: " + Arrays.toString(colorNames.toArray()));


Note: It probably looks odd declaring a "List" and construction an "ArrayList". "ArrayList" is-a "List" type, because "List" is more general we prefer to use it. More on this relationship later. For now, if you just use the above style in your code when you need a list type object, you'll be good to go.

Runnable Examples



References