Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Trouble with generic array at runtime

807580Mar 7 2010 — edited Mar 7 2010
Okay, so basically I need to develop a Java iterator and collection, and the iterator iterates through the collection. To implement the collection, we can use linked lists or arrays (but not arraylist, just traditional arrays), and I went with arrays, because I am still struggling with linked lists and to use them. However, I'm having issues with the generic array thing, because our collection class must be built off of the generic type. I figured out how to make a generic array (with the T[] array = (T[]) new Object[size]; pattern) and how to suppress unchecked error warnings with @SuppressWarnings("unchecked"); however, when I actually try to use this generic array in another class, casting it to a string, it compiles, but I get an error at runtime. (The error is Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.String;) What am I doing wrong?

Here is my code (I realize it's not the best, but I'm a little short on time, so I did my best):
{code}import java.util.*;
import java.io.*;

public class MM06
{
@SuppressWarnings("unchecked")
public static void main(String[] args)
{
CollectionList<String> tester = new CollectionList();
String[] arrayTester = tester.getArray();
for(int i=0;i<arrayTester.length;i++)
{
System.out.println(arrayTester);
}
}
}

class CollectionList<T>
{
@SuppressWarnings("unchecked")
private T[] arrayCollection = (T[]) new Object[5];

/* invariant: size is the number of (non-null) elements in the array */
protected int size = 0;

@SuppressWarnings("unchecked")
public void add(T value)
{
if(arrayCollection.length == size)
{
T[] tmp = (T[]) new Object[arrayCollection.length];
System.arraycopy(arrayCollection,0,tmp,0,arrayCollection.length);
arrayCollection = (T[]) new Object[tmp.length * 2];
System.arraycopy(tmp,0,arrayCollection,0,tmp.length);
}
arrayCollection[size++] = value;
}

//REMOVE LATER - TESTER ONLY
public T[] getArray()
{
return arrayCollection;
}

}
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 4 2010
Added on Mar 7 2010
1 comment
181 views