Problem with Generics

Hi, I was playing around with generics, and started to write some code using them so I can get to be a good friend of them, but I have encountered a problem, and I'm not sure if this can be worked out

I implemented generics in a StaticList, and this is the error I've got:

"Generic array creation"

the code is this

publicclass StaticList<T>{

private T[] elements;

privateint lastIndex;

public StaticList(int max){

this.elements =new T[max];//Here's the problem

this.lastIndex = -1;

}

}

So, I searched about this error on this forums, and found a really old thread (2003) that explains why this can't be done (can't be checked at compile time), but didn't found any workaround. Does someone knows if this has a simple, yet elegant solution? Thanks in advance

[1253 byte] By [shittybytesa] at [2007-11-27 3:54:23]
# 1
I searched a little more, and found this solution, but I don't like the warningsT[] elements = (T[])Array.newInstance(Object.class, max);Is there some way I can create the type-array without getting warnings? :( (without supressing them)
shittybytesa at 2007-7-12 8:58:30 > top of Java-index,Java Essentials,Java Programming...
# 2

The only thing to do it and still use arrays would be to create an Object[] and check on insertion that the element being added is valid. Remember that Object is the universal superclass and any other Object can be stored as one.

This isn't checked by the compiler, and you'll get an unchecked warning, but it's fairly easy to see that only the right types will be able to be stored:

public class StaticList<T> {

private Object[] elements;

private int lastIndex;

public StaticList(int max) {

this.elements = new Object[max]; //Here's the problem

this.lastIndex = -1;

}

public void add(T item){

}

public T get(int x){

return (T)elements[x];

}

}

A better alternative IMO would be to use one of the storage classes in java.util.

Austin_Wa at 2007-7-12 8:58:30 > top of Java-index,Java Essentials,Java Programming...
# 3

I see, but if I can't get rid of the unchecked warnings, I guess it's better to only supress warnings in 1 method, or the constructor in my case, rather than supressing them in every method that returns T

> A better alternative IMO would be to use one of the

> storage classes in java.util.

I know that's the best, but I'm learning generics so I just want to know about them by developing my own containers, thanks anyway :-P

shittybytesa at 2007-7-12 8:58:30 > top of Java-index,Java Essentials,Java Programming...
# 4

I was just checking the source from java of their ArrayList implementation, and I saw this:

/**

* Returns the element at the specified position in this list.

*

* @param index index of the element to return

* @return the element at the specified position in this list

* @throws IndexOutOfBoundsException {@inheritDoc}

*/

public E get(int index) {

RangeCheck(index);

return (E) elementData[index];

}

So I assume this have no elegant solution, or I'm not getting something?

shittybytesa at 2007-7-12 8:58:30 > top of Java-index,Java Essentials,Java Programming...