FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Generic arrays in Java





davidv
I'm implementing a 2,3 tree in Java. The nodes have up to 2 entries and 3 children and I thought it'd be easier to store all entries in an array for later in the searches and removals. My implementation is generic but Java has problems with generic arrays.

This single line is causing warnings:

Code:
entries = (E[]) new Object[2];


Code:
ThreeNode.java:10: warning: [unchecked] unchecked cast
found   : java.lang.Object[]
required: E[]
                entries = (E[]) new Object[2];
                                ^
1 warning


How can I fix this?

EDIT: Storing in an array? Terrible idea. What was I thinking... Embarassed
Fire Boar
You should instead use an Object[] for entries, and cast whenever you access individual elements. The reason it doesn't work the way you want it to has something to do with how generic types are decided only at runtime.
davidv
Fire Boar wrote:
You should instead use an Object[] for entries, and cast whenever you access individual elements. The reason it doesn't work the way you want it to has something to do with how generic types are decided only at runtime.


So, something like this?

Code:
private Object entries[];
//construct
entries = new Object[2];
//getters
public E getSmall() { return (E) entries[0]; }
public E getLarge() { return (E) entries[1]; }
Peterssidan
I haven't used Java much recently so I'm not sure but can't you do it like this?
Code:
E entries[];
entries = new E[3];

This should remove the need for explicit casts.
davidv
Code:
MyThreeNode.java:13: generic array creation
                en = new E[2];
                     ^
1 error


You can't create generic arrays in Java because it's not typesafe so explicit casts are a work around.

http://goo.gl/oi9y9
Peterssidan
Wow, I had forgot how limited Java generics was. Compared to C++ templates, Java generics feels like a hack.

The book you linked to seem to recommend using List<E> instead of array. That way you don't need any casts.
Fire Boar
davidv wrote:
Fire Boar wrote:
You should instead use an Object[] for entries, and cast whenever you access individual elements. The reason it doesn't work the way you want it to has something to do with how generic types are decided only at runtime.


So, something like this?

Code:
private Object entries[];
//construct
entries = new Object[2];
//getters
public E getSmall() { return (E) entries[0]; }
public E getLarge() { return (E) entries[1]; }


Well, almost, except on the first line [] goes after Object, not entries. Smile Essentially though, yes, exactly.

I believe ArrayList<E> also does this behind the scenes. Not sure about that, a debugger would be able to confirm or refute that for sure.
Peterssidan
Fire Boar wrote:
Well, almost, except on the first line [] goes after Object, not entries. Smile
Actually you can do it either way. I think that placing [] directly after the type is more common in Java though.
davidv
If either way works and does the exact same thing then I can say that this also doesn't ensure type safety. The same warning still comes up.
Related topics
Java Game
Java
Add to favorites script - Java
Problems with Java
Java Programming Introductory
Windows to Linux ( Vice Versa )
Language
DSP Tutorial with Java Applets
Programming links, info, and tutorials
Java en soort lijst
problem with java script file
The many Differences between Java and JavaScript
Java - char array
Where should I start? C++ vs. JAVA
Reply to topic    Frihost Forum Index -> Scripting -> Others

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.