How can I initialize a collection and add data on the same line? [duplicate]

If you need a read-only List

List<String> numbers = Arrays.asList("one","two","three");

// Can't add since the list is immutable
numbers.add("four"); // java.lang.UnsupportedOperationException

If you would like to modify the List later on.

List<String> numbers2 = new ArrayList<String>(
                            Arrays.asList("one","two","three"));
numbers2.add("four");

System.out.println(numbers2); // [one, two, three, four]

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)