Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ArrayList Declaration vs Conversion

I'm fairly out of touch with my Java programming and am doing Google's Udacity course as a refresher. I was going through lesson 1 on the Sunshine app where the lecturer chose to create fake data by declaring an array of strings and then converting it to an ArrayList.

The code is the following:

String[] data = {
            "Mon 6/23 - Sunny - 31/17",
            "Tue 6/24 - Foggy - 21/8",
            "Wed 6/25 - Cloudy - 22/17",
            "Thurs 6/26 - Rainy - 18/11",
            "Fri 6/27 - Foggy - 21/10",
            "Sat 6/28 - TRAPPED IN WEATHERSTATION - 23/18",
            "Sun 6/29 - Sunny - 20/7"
    };
    List<String> weatherForecast = new ArrayList<>(Arrays.asList(data));

I was wondering is there any advantage to using this convertion method? Why not just immediately declare the data as an ArrayList as such:

    ArrayList weatherForecast = new ArrayList();
    weatherForecast.add("Today - Sunny - 88/63");
    weatherForecast.add("Tomorrow - Foggy = 70/46");
    weatherForecast.add("Weds - Cloudy - 72/63");
    weatherForecast.add("Thurs 6/26 - Rainy - 18/11");
    weatherForecast.add("Sat 6/28 - TRAPPED IN WEATHERSTATION - 23/18");
    weatherForecast.add("Sun 6/29 - Sunny - 20/7");

Thanks!

like image 200
Patryk Avatar asked Aug 19 '15 10:08

Patryk


People also ask

Can ArrayList be converted to List?

You can convert ArrayList elements to object[] array using ArrayList. ToArray() method.

Can you convert array to ArrayList?

We can convert an array to arraylist using following ways. Using Arrays. asList() method - Pass the required array to this method and get a List object and pass it as a parameter to the constructor of the ArrayList class.

Why are Arraylists better than arrays?

Whereas ArrayList can hold item of different types. An array is faster and that is because ArrayList uses a fixed amount of array. However when you add an element to the ArrayList and it overflows. It creates a new Array and copies every element from the old one to the new one.

Can ArrayList be converted to string?

If you happen to be doing this on Android, there is a nice utility for this called TextUtils which has a . join(String delimiter, Iterable) method. List<String> list = new ArrayList<String>(); list. add("Item 1"); list.


2 Answers

The "normal" way would be to use a third form:

List<String> weatherForecast = new ArrayList<>();
Collections.addAll(weatherForecast,
        "Mon 6/23 - Sunny - 31/17",
        "Tue 6/24 - Foggy - 21/8",
        "Wed 6/25 - Cloudy - 22/17",
        "Thurs 6/26 - Rainy - 18/11",
        "Fri 6/27 - Foggy - 21/10",
        "Sat 6/28 - TRAPPED IN WEATHERSTATION - 23/18",
        "Sun 6/29 - Sunny - 20/7");

List is an interface, using that will enable to use another implementaton class like LinkedList (for just a couple of elements). Also useful as parameter type.

This uses the Collections utility class to add varargs ... strings. And uses under the hood a String[] for those args.

The .asList of the first option would be sufficient to have a List<String> not to be added to.

like image 199
Joop Eggen Avatar answered Oct 04 '22 07:10

Joop Eggen


First approach :

  • Better readability (can be even better, see my alternative); all datas are separated directly by comma instead of having a method call to add each one.
  • Arrays.asList Create a fixed-size list for the given specific array using it internal ArrayList class which is just a wrapper around the array itself that make it more efficient then creating a full copy of the array.
  • You keep the data in a List variable instead of an ArrayList variable which gives you more flexibility.

Second approach :

  • You are using raw types, it's bad at the beginning.
  • No advantages.

Please if you use that approach, at least declare your List in a generic manner :

ArrayList<String> weatherForecast = new ArrayList<>();

Alternative :

Note that you could directly do :

List<String> weatherForecast = Arrays.asList(
    "Mon 6/23 - Sunny - 31/17",
    "Tue 6/24 - Foggy - 21/8",
    "Wed 6/25 - Cloudy - 22/17",
    "Thurs 6/26 - Rainy - 18/11",
    "Fri 6/27 - Foggy - 21/10",
    "Sat 6/28 - TRAPPED IN WEATHERSTATION - 23/18",
    "Sun 6/29 - Sunny - 20/7"
);
like image 43
Jean-François Savard Avatar answered Oct 04 '22 06:10

Jean-François Savard