0

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?

golosovsky
  • 638
  • 1
  • 6
  • 19
  • Did you pay for this Android course? From the code snippet you posted I certainly wouldn't define a `String[]` with string literals in code like that. – Squonk Mar 01 '15 at 12:17
  • @Squonk No, it's a free course. Why are the literarls problematic? Those are just literals of the variable type. – golosovsky Mar 01 '15 at 12:27

5 Answers5

3

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.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • dasblinkenlight thanks for the quick answer! Is there a way to use a only the List<> / ArrayList<> constructor (I mean without the ".add()" calls) without creating this simple "String []" ? – golosovsky Mar 01 '15 at 12:06
  • @golosovsky There are several alternatives described in this other Q&A ([link](http://stackoverflow.com/q/1005073/335858)). – Sergey Kalinichenko Mar 01 '15 at 12:35
1

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
Crazyjavahacking
  • 9,343
  • 2
  • 31
  • 40
1

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");
            }
 };
VeLKerr
  • 2,995
  • 3
  • 24
  • 47
0

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.

  • Is there a way to use a only the List<> / ArrayList<> constructor (I mean without the ".add()" calls) without creating this simple "String []" ? – golosovsky Mar 01 '15 at 12:10
0

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.

Nino DELCEY
  • 652
  • 1
  • 9
  • 25