File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes new ArrayList<Integer>({1,2,3,4}) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "new ArrayList<Integer>({1,2,3,4})" Watch "new ArrayList<Integer>({1,2,3,4})" New topic
Author

new ArrayList<Integer>({1,2,3,4})

nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
Hello,

How can I add some values in a List while I instantiate it?

Something like:

List<Integer> a = new ArrayList<Integer>({1,2,3,4});

I do not want to use:

a.add(1);
a.add(2);
..
Rahul P Kumar
Ranch Hand

Joined: Sep 26, 2009
Posts: 188
See constructor of arraylist that what is allowed to pass in the constructor.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

There is no constructor in Collection classes which takes an array. You can use java.util.Arrays.asList method to get a fix sized list of the array. But don't pass a primitive array to the asList method otherwise you won't get the desired result...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
So I can do this:



Is that right?

What do you mean with

otherwise you won't get the desired result...


Does the Arrays.asList-Method remains the sequence? I do not want that the sequence "1,2,4,5" changes.

Rahul P Kumar
Ranch Hand

Joined: Sep 26, 2009
Posts: 188
nimo frey wrote:



Is that right?

No that is not right. It aslist takes array as an argument and you have not given correct implementation of array as an argument. You have just passed some integers separated by comma. Find the way an array is defined implicitly and that needs to be passed as an argument.


nimo frey wrote:
What do you mean with

otherwise you won't get the desired result...


Does the Arrays.asList-Method remains the sequence? I do not want that the sequence "1,2,4,5" changes.



it will retain the order.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3012
    
  10
Rahul.p Kumar wrote:
nimo frey wrote:
Is that right?

No that is not right. It aslist takes array as an argument and you have not given correct implementation of array as an argument. You have just passed some integers separated by comma. Find the way an array is defined implicitly and that needs to be passed as an argument.

I believe you're mistaken, Rahul. The syntax given by Nimo works just fine. It relies on several features that were introduced in JDK 5, such as varargs, autoboxing, and a new method signature for Arrasy.asList(). Try it and see.
nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
I have tried, all works.

thanks.

Rahul P Kumar
Ranch Hand

Joined: Sep 26, 2009
Posts: 188
Thanks Mike to point out that. It works.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19682
    
  20

nimo frey wrote:So I can do this:



Is that right?

What do you mean with

otherwise you won't get the desired result...


Does the Arrays.asList-Method remains the sequence? I do not want that the sequence "1,2,4,5" changes.


The above will work just fine, but the following will not:
Array.asList can except either an Object[] or a number of Objects*. Since int[] is no subtype of Object[] it will treat the entire array as one single element for the new List, and the actual return value is List<int[]>. This is also why you will likely get a compiler warning when you pass a String[] to Arrays.asList (and methods with similar signatures): String[] is both an Object[] and an Object.

* It uses autoboxing to Integer for your code.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Rahul P Kumar
Ranch Hand

Joined: Sep 26, 2009
Posts: 188
Rob Prime wrote:
The above will work just fine, but the following will not:
Array.asList can except either an Object[] or a number of Objects*. Since int[] is no subtype of Object[] it will treat the entire array as one single element for the new List, and the actual return value is List<int[]>. This is also why you will likely get a compiler warning when you pass a String[] to Arrays.asList (and methods with similar signatures): String[] is both an Object[] and an Object.

* It uses autoboxing to Integer for your code.

Arrays.asList(array) alone will compile fine, but putting it in list of Integers won't work. To make it work, you can convert array declaration from int (primitive type) to object type Integer.
Rahul P Kumar
Ranch Hand

Joined: Sep 26, 2009
Posts: 188
Moreover I don't understand why you need to wrap the created list through Arrays util class again in ArrayList, when Arrays.asList() returns a list and that can be assigned to reference variable directly.
 
 
subject: new ArrayList<Integer>({1,2,3,4})