• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generic arrays

 
Roel De Nijs
Sheriff
Posts: 9934
113
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(question about java tiger)

You can't create generic arrays for some reason. I was wondering if there is a workaround for something like

ArrayList<TestA>[] testTabel = new ArrayList<TestA>[2];

What do you have to do if you really need something like that?
 
Stuart Ash
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean generic arrays? Arrays already have type mentioned in them.
 
Roel De Nijs
Sheriff
Posts: 9934
113
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Stuart Ash:
What do you mean generic arrays? Arrays already have type mentioned in them.


A normal array declaration would be something like:
ArrayList[] testTabel = new ArrayList[2];
// a table where each element is an arraylist and in the arraylist you can add any object you want

A generic array is something like:
ArrayList<TestA>[] testTabel = new ArrayList<TestA>[2];
// a table where each element is an arraylist and in the arraylist you can only add instances of class TestA
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, you cannot do that. See Angelika Langer's Generics FAQ
 
Roel De Nijs
Sheriff
Posts: 9934
113
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Barry Gaunt:
No, you cannot do that. See Angelika Langer's Generics FAQ


But you can make an ArrayList<ArrayList<TestA>> instead of the table mentioned. Or isn't that good practice?
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Roel De Nijs:


But you can make an ArrayList<ArrayList<TestA>> instead of the table mentioned. Or isn't that good practice?


That, to me, is a perfectly good and safe way to do it.
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ArrayList<TestA>[] testTabel = new ArrayList<TestA>[2];


Actually if you put a "()" after the "<TestA>" at the end like this

ArrayList<TestA>[] testTabel = new ArrayList<TestA>()[2];


you will create an Array containing two ArrayLists. Meaning the Type of the array is ArrayList. just like

String[] a = new String[2];

makes that an Array of Strings. See in the case of Arrays, the Type that is before the [] is the type of the array and why you don't need to make them generic. They already are.

Mark
 
Krishna Latha Grandhi
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I feel generic arrays belongs to 1.5 . Am I right?
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[B][Mark S]: Actually if you put a "()" after the "<TestA>" at the end like this

you will create an Array containing two ArrayLists.[/B]

I don't understand this, and I just get a compile error when I try it.

[Hari]: I feel generic arrays belongs to 1.5 . Am I right?

This thread has been about 1.5 all along, and there is still no such thing as a generic array. Barry's answer links to an explanation why.
 
Roel De Nijs
Sheriff
Posts: 9934
113
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Hari Krishna:
I feel generic arrays belongs to 1.5 . Am I right?


That's why i put in my first post between brackets "question for Java Tiger", so people who are preparing for scjp1.4 don't get a heart attack thinking they have missed something
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic