• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Instantiating an Array of Lists

 
Ranch Hand
Posts: 244
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I wrote a little Java program:

When I compile it I get the message:

LiAr.java:10: error: cannot create array with '<>'
lisArr = new List<>[ 5];
^
1 error

The compiler is complaining about line 10, not line 9, so it appears it's legal to have an array of objects of type {List< Integer>}. How then can I instantiate the array? The way I tried is definitely not working.
 
Marshal
Posts: 76884
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, you cannot create an array of anything with <> in its name. Go through the Java™ Tutorials and you may find the explanation. It has to do with generics being invariant and arrays being covariant, so you get type1[] being a subtype of type2[] even though type1 is not a subtype of type2.
Avoid trying to put Lists into arrays; put them into Lists.You may be able to miss out the word Integer from that instantiation.
 
Sheriff
Posts: 22701
129
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

Campbell Ritchie wrote:


The assignment should use ArrayList<List<Integer>> (or shorter, just ArrayList<>). You can still put LinkedLists inside it though.
 
Campbell Ritchie
Marshal
Posts: 76884
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, you are right there, Rob. Thank you.
 
Rob Spoor
Sheriff
Posts: 22701
129
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
You're welcome.
 
Ranch Hand
Posts: 239
12
Scala IntelliJ IDE Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
LOL! Liar! Nice class name!

If you really want to dance on the wild side you can just forget about parameterizing it at all.



The compiler also accepts "<Object>" as its type parameter.

As mentioned above, either seems like a bad idea when you can have a list of lists.
 
Our first order of business must be this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic