• 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
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

arrays of ArrayList confusion

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can we declare arrays of arrays of ArrayList?

i.e. ArrayList<String> myList=new ArrayList<String>[5]; // is this valid ??
 
Marshal
Posts: 73743
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No.

I can't remember why, but a search of this forum will reveal old posts about the same subject.
You cannot create an array of parameterised type. So List[] myLists = new ArrayList[3]; is permissible. But List<Foo>[] myFooLists = new ArrayList<Foo>[3]; is not permissible. It has something to do with erasure rather than reification in generics, but it is better explained in the older posts.
 
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Rajat Sarkar:
Can we declare arrays of arrays of ArrayList?

i.e. ArrayList<String> myList=new ArrayList<String>[5]; // is this valid ??



Not like that, but yes, you can create arrays of Arraylists:



But I wouldn't advise to do it like that. The (rather ugly) casting will also cause the compiler to cough up warning(s) (not errors!). So I advise to use List<List<String>>, or something similar instead.
[ October 15, 2008: Message edited by: Piet Verdriet ]
 
My first bit of advice is that if you are going to be a mime, you shouldn't talk. Even the tiny ad is nodding:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic