Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

initializing ArrayList

 
Bill Jones
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?
 
Freddy Wong
Ranch Hand
Posts: 959
Eclipse IDE Java Linux
  • 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
 
Garrett Rowe
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:
 
didi Dji
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 ^__^
 
Manuel Leiria
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about,

 
Evelina Vrabie
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)
 
Larry Swanson
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
 
Rob Spoor
Sheriff
Pie
Posts: 20529
54
Chrome Eclipse IDE Java 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.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15284
39
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).
 
Anish Kurian Thomas
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 :
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
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:


 
Vinay M Raju
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Very useful information. Thansk a lot !!
 
Randall Twede
Ranch Hand
Posts: 4399
3
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
very interesting topic but i would have just used an array
 
Winston Gutkowski
Bartender
Pie
Posts: 10417
63
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
 
Stephan van Hulst
Bartender
Pie
Posts: 5812
61
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic