Java-Gaming.org Hi !
Featured games (81)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (576)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  ArrayList[] with generics not compiling...  (Read 1002 times)
0 Members and 1 Guest are viewing this topic.
Offline theagentd
« Posted 2013-04-02 14:28:11 »

I just can't seem to create an array of ArrayLists with generics.

1  
2  
3  
4  
5  
private ArrayList<FloorInstance>[] floorBatches;

...

floorBatches = new ArrayList<>[numFloorMeshes];

This gives me this error:
Quote
Incorrect number of arguments for type ArrayList<E>; it cannot be parameterized with arguments <>

So I add the so called missing parameter:
1  
floorBatches = new ArrayList<FloorInstance>[numFloorMeshes];

and I get this error instead:
Quote
Cannot create a generic array of ArrayList<DungeonRenderer3.Chunk.FloorInstance>

This has been nagging me for a while now...

Myomyomyo.
Offline sproingie

JGO Kernel


Medals: 202



« Reply #1 - Posted 2013-04-02 14:38:00 »

You cannot create an array of generics, because arrays are covariant and generics are (normally) invariant.  Combined with the fact that arrays are reified and generics are erased, this makes them incompatible that way -- the array can't store the actual generic type anymore. You can however have a generic parameterized with an array since java can check the cast.  

See here: http://docs.oracle.com/javase/tutorial/extra/generics/fineprint.html
More gory details are here: http://www.angelikalanger.com/Articles/Papers/JavaGenerics/ArraysInJavaGenerics.htm

(edit: sorry, posted before my first coffee of the day and I reversed the types you were allowed to create)
Offline StrideColossus
« Reply #2 - Posted 2013-04-02 14:40:59 »

You cannot create an array of a generic type, it's an inherent limitation (or perhaps restriction is a better term) of the compiler.  If you do a google search for "array of arraylist" you'll see lots of people with the same question.

There are a couple of nasty workarounds:
- use Array.newInstance() and cast the result
- use ArrayList<ArrayList<Thing>>( size );

But you should probably consider a redesign that doesn't require you to 'mix' an array of lists.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Roquen
« Reply #3 - Posted 2013-04-02 14:43:11 »

I must resist....I cannot!!  Type erasure sucks.
Offline Pauler
« Reply #4 - Posted 2013-04-02 14:46:07 »

1  
2  
3  
private ArrayList<FloorInstance>[] floorBatches;

floorBatches = new ArrayList<>[numFloorMeshes];


to

1  
2  
3  
private ArrayList<FloorInstance>[] floorBatches;

floorBatches = new ArrayList<FloorInstance>[numFloorMeshes];

Offline theagentd
« Reply #5 - Posted 2013-04-02 15:21:43 »

Thanks for the information, guys.

I must resist....I cannot!!  Type erasure sucks.
Agreed.

@Pauler
That doesn't work either.

Myomyomyo.
Offline matheus23

JGO Kernel


Medals: 109
Projects: 3


You think about my Avatar right now!


« Reply #6 - Posted 2013-04-02 15:36:40 »

Thanks for the information, guys.

I must resist....I cannot!!  Type erasure sucks.
Agreed.

@Pauler
That doesn't work either.

One argument for scala:

There is the implicit Manifest (before 'ClassManifest'), the compiler injects it at compile time and it stores type information Wink

Also, you can create Generic arrays in scala Tongue

See! Cheesy

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline quew8

JGO Coder


Medals: 31



« Reply #7 - Posted 2013-04-02 17:01:29 »

Quote
I must resist....I cannot!!  Type erasure sucks.
I used to agree with you until I saw c++ templates. If you think type erasure is bad, just try to declare a generic variable in the global scope. fyi, you cannot. That sir, is what really sucks here. Give me type erasure any day.
Offline theagentd
« Reply #8 - Posted 2013-04-02 17:02:17 »

@quew8
Everything's relative. Tongue

Myomyomyo.
Offline Roquen
« Reply #9 - Posted 2013-04-02 17:30:30 »

templates and generics have nothing in common.  The first is a macro and the second is a contract.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 816
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #10 - Posted 2013-04-02 20:57:21 »

I just create a non-generic array and cast it to a generic one. Then suppress the warning. persecutioncomplex


I reject the theories of why and how this is bad style. I need to get work done.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline ra4king

JGO Kernel


Medals: 350
Projects: 3
Exp: 5 years


I'm the King!


« Reply #11 - Posted 2013-04-02 22:30:50 »

I need to get work done.
Excellent quote.

A cute workaround for everyone:
1  
2  
3  
4  
5  
6  
7  
8  
9  
public static <T> T[] newArray(int size, T ... t) {
    return Arrays.copyOf(t, size);
}

...

V[] arrayOfGenerics = newArray(mySize);

ArrayList<MyType>[] myList = newArray(myOtherSize, new ArrayList[]{});


Offline SHC
« Reply #12 - Posted 2013-04-03 03:22:11 »

A nice work around. Thanks for sharing.

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 816
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #13 - Posted 2013-04-03 04:13:46 »

That convenient method creates an intermediate dummy array.

Whether that's okay, depends on the use case.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline quew8

JGO Coder


Medals: 31



« Reply #14 - Posted 2013-04-03 15:07:45 »

Quote
templates and generics have nothing in common.  The first is a macro and the second is a contract
I know they're not the same thing, they are however often used for equivalent purposes.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Longarmx (38 views)
2014-10-17 03:59:02

Norakomi (29 views)
2014-10-16 15:22:06

Norakomi (24 views)
2014-10-16 15:20:20

lcass (28 views)
2014-10-15 16:18:58

TehJavaDev (56 views)
2014-10-14 00:39:48

TehJavaDev (55 views)
2014-10-14 00:35:47

TehJavaDev (45 views)
2014-10-14 00:32:37

BurntPizza (64 views)
2014-10-11 23:24:42

BurntPizza (36 views)
2014-10-11 23:10:45

BurntPizza (78 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!