• 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
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Arrays of generic objects

 
Ranch Hand
Posts: 35
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I define an array of String-typed JComboxs, Choices, as

JComboBox<String>[] Choices ;

knowing the actual array length, N, won't be available later.

When I know it, I'd like to put

Choices = new JComboBox<String>[N];

but this provokes 2 errors,

Type safety: The expression of JComboBox[] needs unchecked conversion to conform to type JComboBox<String>[].

Cannot create a generic array of JComboBox<String>

Given that I know the array Choices is JComboBox<String>, how do I define Choicfes, then set the array length later?

Thanks,

Chris Kimball
 
Bartender
Posts: 1464
32
Netbeans IDE C++ Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Last I looked, Chris, you just can't have arrays of generic classes. Java can't do it. But, you can get a close approximation with ArrayList objects:


Does that help?

(By the way, universal practice in Java is to use a lower-case letter as the first letter of all of your variables. Class names (and interfaces, and enums) start with caps.)

 
Chris Kimball
Ranch Hand
Posts: 35
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to all! The ArrayList approach works well.






Chris
 
Stevens Miller
Bartender
Posts: 1464
32
Netbeans IDE C++ Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Great! And you found a constructor that simplifies things for you: also great!

I've edited your comment to useCodeTags. Again, please use lower-case in the first letter of your variable names. That's how everyone does it, and it will confuse others if you don't.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Chris Kimball wrote:When I know it, I'd like to put
Choices = new JComboBox<String>[N];


I should add, for completeness, that you can do what you want with:

  choices = (JComboBox<String>[]) new JComboBox[N];[

but you will still get the warning about unchecked conversion.

You can remove that with something like:which would then allow you to use a native array; but TBH, I prefer Stevens' suggestion.

It's an unfortunate fact that arrays and generics just don't "mix" very well; but for 99.99% of the things you're ever likely to do, the overhead of an ArrayList - or indeed a List in general - is so miniscule that you'll probably never notice it.

And with a List, you get all sorts of other goodies like Iterators and sorts and contains() to play with, so my advice is to get used to using them ... for most stuff.

HIH

Winston
 
Marshal
Posts: 76416
365
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:. . . And with a List, you get all sorts of other goodies . . .

That is why so many people on this website advise you to move from arrays to Lists as soon as possible.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic