• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

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

 
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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);
..
 
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See constructor of arraylist that what is allowed to pass in the constructor.
 
Sheriff
Posts: 9692
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Master Rancher
Posts: 4197
57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have tried, all works.

thanks.

 
Rahul P Kumar
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Mike to point out that. It works.
 
Sheriff
Posts: 22649
126
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Rahul P Kumar
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
He loves you so much! And I'm baking the cake! I'm going to put this tiny ad in the cake:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic