This week's book giveaway is in the Go forum.
We're giving away four copies of Head First Go and have Jay McGavren on-line!
See this thread for details.
Win a copy of Head First Go this week in the Go forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Devaka Cooray
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Tim Holloway
  • Claude Moore
  • Stephan van Hulst
Bartenders:
  • Winston Gutkowski
  • Carey Brown
  • Frits Walraven

initializing ArrayList  RSS feed

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am missing something simple and fundamental. Maybe you guys can help.

With an "old-style" array, I can initialize it like this:
int[] myArray = new int[]{ 2000, 100, 40, 60};

With an ArrayList, it seems like I have to do
ArrayList<Integer> myArray = new ArrayList<Integer>();
myArray.add(2000)
myArray.add(100)
myArray.add(40)
myArray.add(60)
for each element that I want to add. Is there a way that I can add any number of integers to my ArrayList in a single line of code?

Any ideas?
 
Ranch Hand
Posts: 959
Eclipse IDE Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As far as I know, there isn't a way to initialize the ArrayList as what you normally do with Java array. But of course, there's nothing stopping you from creating a method to do such a thing
For example:
 
Bill Jones
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're the man, Freddy Wong. Here is what I did that worked. Since I won't always know the number of integer elements in the array, I decided to pass in an "old-style" array to the initArrayList method. Thank you!

main:
ArrayList<Integer> note = new ArrayList<Integer>();
int[] myArray = new int[]{ 58,63,67,72,70,63,62,63 };
note = initArrayList(myArray);


method:
public ArrayList<Integer> initArrayList(int[] a) {
ArrayList<Integer> list = new ArrayList<Integer>();
for (int i : a) {
list.add(i);
} //close for loop
return list;
} //close initArrayList method
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can also do this:

But note the List returned from Arrays.asList() is fixed size. If you need a non fixed-size List you can do this:
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know that the post is old but I just wanted to make BIG thanks to Freddy Wong and specially to Garrett Rowe for their replies cause it was EXACLTY what I was looking for . I was so happy to find it cause I've been searching for more than 1 hour on the API of sun and on google without finding something very nice x).I'm very happy and I just wanted to say :

Thank you a loooooot ^__^
 
Ranch Hand
Posts: 171
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about,

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Manuel Leiria wrote:How about,



Or ArrayList<Integer> arr = new ArrayList<Integer>() {{ add(30); add(40); }}; Same syntax can be used with Map (use put instead of add)
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This thread is ancient, but here's another method I saw in a seminar with Adam Bien.



I'm not quite sure what's going on here though...

Larry
 
Sheriff
Posts: 21653
101
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It creates an anonymous subclass of ArrayList (the outer pair of {}). This anonymous subclass has an initializer block (the inner pair of {}) which executes the add methods. It's a nasty little hack that should be avoided.
 
Java Cowboy
Posts: 16084
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That trick is called "double brace initialization". I agree with Rob that it's a dirty trick that should be avoided.

(Note that Evelina Vrabie already mentioned this trick in her post of April 9, 2009).
 
Greenhorn
Posts: 21
Java Oracle Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is another way you can do this :
 
author and iconoclast
Posts: 24203
43
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Anish Kurian Thomas wrote:There is another way you can do this :



You could do that, but only if you enjoy ClassCastExceptions, because that's what you'd get. Arrays.asList() does not return an ArrayList.
 
Anish Kurian Thomas
Greenhorn
Posts: 21
Java Oracle Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry .. I was wrong.. Thank you Ernest Friedman-Hill for pointing this out.

I believe the code will work by changing into:


 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Very useful information. Thansk a lot !!
 
Ranch Hand
Posts: 4702
9
Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
very interesting topic but i would have just used an array
 
Bartender
Posts: 10759
68
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Vinay M Raju wrote:Very useful information. Thansk a lot !!


It should be pointed out that Anish's code is redundant. What Ernest was trying to highlight is that Arrays.asList() returns a List, not an ArrayList (actually, the List is an ArrayList - at least right now - but you shouldn't take it for granted).

It's almost invariably best to use interfaces anyway, so far better is:
List<Integer> myArray = Arrays.asList(1,2,3);

Randall Twede wrote:very interesting topic but i would have just used an array


Then you're missing out. Apart from fixed-length primitive arrays and String.split(), I hardly ever use them any more.

Winston
 
Saloon Keeper
Posts: 9869
199
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you really must have an ArrayList, what you can also do is the following:
This makes use of the fact that every concrete collection type is supposed to have a constructor that copies the elements from an existing collection; in this case the new ArrayList copies the elements from the List returned by the asList() method.

[edit]

Wow, never mind, I completely missed Anish's example.
 
A berm makes a great wind break. And Iwe all like to break wind once in a while. Like this tiny ad:
Become a Java guru with IntelliJ IDEA
https://www.jetbrains.com/idea/
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!