• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Generic arrays

 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do you mean generic arrays? Arrays already have type mentioned in them.
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, you cannot do that. See Angelika Langer's Generics FAQ
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I feel generic arrays belongs to 1.5 . Am I right?
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic