golosovsky
golosovsky

Reputation: 718

ArrayList<>/List<> vs using String array and conerting it to ArrayList<>

I'm currently taking an Android development course by Google on Udacity.

I've seen a code example which I do not completely understand (just a basic Java question):

String strarrWeek [] = {
            "Sunday",
            "Monday",
            "Tuesday",
            "Wednesday",
            "Thursday",
            "Friday",
            "Saturday"
        };

List<String> lstWeek = new ArrayList<String>(Arrays.asList(strarrWeek));

What's the reason to do this conversion if you can just:

ArrayList<String> arlstWeek = new ArrayList<String>();
        arlstWeek.add("Sunday");
        arlstWeek.add("Monday");
        arlstWeek.add("Tuesday");
        arlstWeek.add("Wednesday");
        arlstWeek.add("Thursday");
        arlstWeek.add("Friday");
        arlstWeek.add("Saturday");

Am I missing something?

Thanks.

+edit: Is there a way to use a only the List<> / ArrayList<> constructor (I mean without the ".add()" calls), without creating the "String []" array?

Upvotes: 0

Views: 113

Answers (5)

VeLKerr
VeLKerr

Reputation: 3157

In 2nd part of example you can use shorter syntax:

ArrayList<String> arlstWeek = new ArrayList<String>(){
            {
                add("Sunday");
                add("Monday");
                add("Tuesday");
                add("Wednesday");
                add("Thursday");
                add("Friday");
                add("Saturday");
            }
 };

Upvotes: 1

Crazyjavahacking
Crazyjavahacking

Reputation: 9697

All of the answers are a little bit misleading and not 100% technically correct:

String strarrWeek [] = {
        "Sunday",
        "Monday",
        "Tuesday",
        "Wednesday",
        "Thursday",
        "Friday",
        "Saturday"
};

This code:

  • uses a shortcut syntax for array initialization right in the declaration
  • creates an array with exactly 7 items
  • arrays in Java are always mutable data strutures

List<String> lstWeek = new ArrayList<String>(Arrays.asList(strarrWeek)); This code:

  • will first create an instance of fixed-sized List that will sync the changes in list back in the array
  • then create additional List instance by iterating over the first List and adding all elements into the newly created instance

ArrayList<String> arlstWeek = new ArrayList<String>();
    arlstWeek.add("Sunday");
    arlstWeek.add("Monday");
    arlstWeek.add("Tuesday");
    arlstWeek.add("Wednesday");
    arlstWeek.add("Thursday");
    arlstWeek.add("Friday");
    arlstWeek.add("Saturday");

This code:

  • will create empty mutable instance of ArrayList with default size and put all the values inside the collection

Upvotes: 1

Nino DELCEY
Nino DELCEY

Reputation: 678

It keeps the order of the elements and it may be about efficiency since .add() method will make the Array grow in memory for every call.

It's only guesses of course, this is the only pros I can think about.

Upvotes: 0

Grzegorz Fedczyszyn
Grzegorz Fedczyszyn

Reputation: 344

In first example you have only two function calls, in second you have n function calls. I would prefer the first approach- the less the application have to do the less often it will fail.

Upvotes: 0

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726639

The major difference is that the first code snippet can be placed directly into a declaration section of your code (i.e. outside of constructors and methods), while the second code snippet must be within a code block. This is because the first snippet uses only initialization constructs, while the second mixes initialization (the constructor) and execution (the calls of add).

The first construct uses an array and a list wrapper that both get dropped, so it is not as memory efficient as the second one. The second version could be improved by requesting that ArrayList be initialized with the exact number of items before adding strings to it:

ArrayList<String> arlstWeek = new ArrayList<String>(7);
//                                                  ^

This would prevent re-allocations when the collection grows as you add elements to it.

Upvotes: 3

Related Questions