Reputation: 718
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
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
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:
List<String> lstWeek = new ArrayList<String>(Arrays.asList(strarrWeek));
This code:
List
that will sync the changes in list back in the arrayList
instance by iterating over the first List
and adding all elements into the newly created instanceArrayList<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:
ArrayList
with default size and put all the values inside the collectionUpvotes: 1
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
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
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