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!
You can convert ArrayList elements to object[] array using ArrayList. ToArray() method.
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.
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.
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.
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.
First approach :
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.List
variable instead of an ArrayList
variable which gives you more flexibility.Second approach :
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"
);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With