What's the way to initialize a line in ArrayList?

Asked 2 years ago, Updated 2 years ago, 86 views

    ArrayList<String> places = new ArrayList<String>();
    places.add("Buenos Aires");
    places.add("Córdoba");
    places.add("La Plata");

For example, there is a source like this, and I want to reduce the initialization syntax to one line, is there a good way?

java collections arraylist initialization

2022-09-22 22:38

1 Answers

I think it'd be good to do it like this.

ArrayList<String> places = new ArrayList<String>(
Arrays.asList("Buenos Aires", "Córdoba", "La Plata"));


2022-09-22 22:38

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.