• Post Reply Bookmark Topic Watch Topic
  • New Topic

Arrays of generic objects  RSS feed

 
Ranch Hand
Posts: 35
Java
  • Mark post as helpful
  • send pies
  • 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: 1445
30
C++ Java Netbeans IDE Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thanks to all! The ArrayList approach works well.






Chris
 
Stevens Miller
Bartender
Posts: 1445
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • 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: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • 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: 56600
172
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!