Problem with generic array copying
Goal
I am making a Java class that will give enhanced usability to arrays, such as add
, remove
, and contains
methods. I figured the best solution is to make a class (called ArrayPP
) that has a type parameter T
. This way, the user can interact with the ArrayPP
object as easily as they can with an array of the same type.
Problem
I quickly found that such methods as add
will require the creation of a separate array, and end up changing the target array t
from an array of T
s into an array of Object
s. As you may guess, this totally destroys the usability, and when I try to do something like
File[] f = new File[0];
ArrayPP<File> appF = new ArrayPP(f);
appF.add(saveFile);
f = appF.toArray();
the program throws
Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.io.File;
because the add
method has to change the array into an array of Object
s, as the Java compiler won't let you make a generic array (T[] t = new T[0];
is bad, but T[] t = (T[]) new Object[0];
is okay). I know from line-by-line debugging that the above code keeps the array t
, in this case, as a n array of File
s UNTIL the 4th line of the add
method is called. Does anyone have a solution that will keep the array t
being an array of T
s and not an array of Object
s?
Sample Code
Below is a VERY watered-down version of my class.
public class ArrayPP<T>
{
T[] t;
/**
* Creates a new Array++ to manage the given array.
* <h3>Analogy:</h3>
* <tt>ArrayPP<String> s = new ArrayPP(args);</tt><br/>
* is analogous to<br/>
* <tt>String s[] = args;</tt>
* @param array The array to be managed
*/
开发者_开发技巧public ArrayPP(T[] array)
{
t = array;
}
/**
* Appends a value to the end of the array
* @param val the value to be appended
* @return the resulting array.
*/
public ArrayPP add(T val)
{
T[] temp = (T[]) new Object[t.length + 1];
System.arraycopy(t, 0, temp, 0, t.length);
temp[temp.length - 1] = val;
t = (T[])temp;
return this;
}
/**
* Returns the array at the core of this wrapper
* @return the array at the core of this wrapper
*/
public T[] toArray()
{
return t;
}
}
Possible Solution?
After looking at other questions about generic arrays, I think I have a solution:
Instead of
/**
* Appends a value to the end of the array
* @param val the value to be appended
* @return the resulting array.
*/
public ArrayPP add(T val)
{
T[] temp = (T[]) new Object[t.length + 1];
System.arraycopy(t, 0, temp, 0, t.length);
temp[temp.length - 1] = val;
t = (T[])temp;
return this;
}
will this work?
/**
* Appends a value to the end of the array
* @param val the value to be appended
* @return the resulting array.
*/
public ArrayPP<T> add(T val)
{
t = java.util.Arrays.copyOf(t, t.length + 1);
t[t.length - 1] = val;
return this;
}
In principle you can't easily create arrays of a generic type (or type variable).
If you have a class object, you can use reflection, or if you have an example array, the methods in the java.util.Arrays
class to create a (longer/shorter) copy. But it is not elegant either way.
The ArrayList class internally simply uses an Object[]
to store its elements, and converts only on get/set/add/toArray.
What would your class do better than ArrayList?
Edit:
I would recommend either simply delegate to an ArraysList, or do the implementation like ArrayList does, using an Object[]
internally, and converting on output where necessary.
If you really want to have an array of the right type internally, it is possible - but it gets ugly, as I said.
The add
method is still the easiest case:
/**
* Appends a value to the end of the array
* @param val the value to be appended
* @return the resulting array.
*/
public ArrayPP add(T val)
{
T[] temp = Arrays.copyOf(t, t.length+1);
temp[t.length] = val;
t = temp;
return this;
}
When you want to add in the middle or remove, you'll have to combine this with your arraycopy.
Is there some reason the built-it List<T>
class(es) can't do what you need? As in:
String[] theArray = {"a", "b", "c"};
List<String> theList = Arrays.asList(theArray);
public ArrayPP(T[] array)
componentClass = array.getClass().getComponentClass();
T[] newArray(int length)
return Array.newInstance(componentClass, length)
精彩评论