• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Populating an ArrayList with one statement

 
Ranch Hand
Posts: 86
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm trying to fill an ArrayList without having to call the add method a bunch of times, but the populating rules are apparently different from regular arrays:

The error on line 22 says, "Type mismatch: cannot convert from String[] to ArrayList<String>".

The error on line 23 says, "Cannot infer type arguments for ArrayList<>".

Is it possible to populate an ArrayList with one line of code?
 
Marshal
Posts: 74020
332
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In line 22 you are declaring a List (please write List<String> like line 23), but then assigning it from an array initialiser. Find the Arrays#asList() method, or List#of(), but read both carefully because they don't produce an ordinary List. You can of course pass the returned value to the ArrayList() constructor.
I am not quite sure why you are getting that particular error messge for line 23, but I don't think ArrayList has a constructor requiring six parameters.
 
Bartender
Posts: 2880
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sam Peterson wrote:

Can you explain what the above statement should do ?
Also note that these are the current constructors for ArrayList in java8:

javadocs wrote:ArrayList()
Constructs an empty list with an initial capacity of ten.
ArrayList(Collection<? extends E> c)
Constructs a list containing the elements of the specified collection, in the order they are returned by the collection's iterator.
ArrayList(int initialCapacity)
Constructs an empty list with the specified initial capacity.

 
Sam Peterson
Ranch Hand
Posts: 86
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is there a way to create an array that avoids the backed list effect? The fact that I cannot add any elements to the birds4 list (line 28) leads me to suspect that using the Arrays.asList method isn't creating an ArrayList, but just another array.
Am I wrong? Is line 24 a regular array:

Output:
birds size = 6
birds = [hawk, crow, eagle, falcon, falcon2, falcon3]
birds4 size = 6
birds4 = [hawk, crow, eagle, falcon, falcon2, falcon3]
birds == birds4 is true
Exception in thread "main" java.lang.UnsupportedOperationException
at java.util.AbstractList.add(Unknown Source)
at java.util.AbstractList.add(Unknown Source)
at Chapter3.SamsArrayLists.main(SamsArrayLists.java:28)
 
Campbell Ritchie
Marshal
Posts: 74020
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have successfully created a List with asList() only to find it is unmodifiable. But you an copy it with the new XYZList(...) constructor. Most List implementations will probably implement such a constructor. asList()
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic