wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Generic arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Generic arrays" Watch "Generic arrays" New topic
Author

Generic arrays

Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5278
    
  13

(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?


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
Stuart Ash
Ranch Hand

Joined: Oct 07, 2005
Posts: 637
What do you mean generic arrays? Arrays already have type mentioned in them.


ASCII silly question, Get a silly ANSI.
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5278
    
  13

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

Joined: Aug 03, 2002
Posts: 7729
No, you cannot do that. See Angelika Langer's Generics FAQ


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5278
    
  13

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

Joined: Aug 03, 2002
Posts: 7729
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

Joined: Feb 05, 2001
Posts: 17250
    
    6

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


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Krishna Latha Grandhi
Ranch Hand

Joined: Nov 08, 2005
Posts: 110
I feel generic arrays belongs to 1.5 . Am I right?
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[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.


"I'm not back." - Bill Harding, Twister
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5278
    
  13

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generic arrays