我想实现一种基于泛型的java方法,将List<T>
转换为T[]
。
例如,我有一个List<T>
,例如List<Double>
:
List<Double> lst = Arrays.asList( new Double[]{1.11, 2.22, 3.33} );
我有以下方法:
public static <T> T[] listToArray(List<T> inputList)
{
@SuppressWarnings("unchecked")
Class<T> type = (Class<T>)NumberTypeOfString(inputList.get(0).toString());
@SuppressWarnings("unchecked")
T[] resultArray = (T[]) Array.newInstance(type, inputList.size());
for(int i=0; i < inputList.size(); i++)
{
resultArray[i] = (T)inputList.get(i);
}
return resultArray;
}
当我将此方法称为以下内容时:
Double[] resultArray = listToArray(lst);
我得到一个我真的无法理解的例外:
java.lang.ArrayStoreException: java.lang.String
为什么会这样?
PS:上面的方法“NumberTypeOfString”是输入类似“1.11”的字符串来获取类似Double.class的类型,对于其他数字类型也是如此。
编辑:
我将删除第二个问题(“有没有办法正确实现该方法?”)以获得对此代码的更多关注。
解决方案&amp;深刻的问题:
Just use List<Double> and nothing goes wrong at all.
但实际上,我有一种生成AP或GP的方法,它返回一个ArrayList:
ArrayList<Double> arrayOfResult = createArrayOfProgression("ratio; 1; 2; 12;12");
其中,“比率; 1; 2; 12; 12”表示选择;第一;步;持续;总数。
arrayOfResult的内容是:
[1.0, 2.0, 4.0, 8.0, 16.0, 32.0, 64.0, 128.0, 256.0, 512.0, 1024.0, 2048.0]
调用它的结果是一个ArrayList,但是当这个arrayOfResult稍后输入到listToArray中时会引起上述异常。 这是代码:
public enum caculator{
diff, ratio;
}
public static <T> ArrayList<T> createArrayOfProgression(String firstStepLastTotal)
{
String[] arrayOfParameter = firstStepLastTotal.replace(" ", "").split(";");
String choice = arrayOfParameter[0];
double first = Double.valueOf(arrayOfParameter[1]);
double step = Double.valueOf(arrayOfParameter[2]);
String strlast = arrayOfParameter[3];
String strtotal = "";
if(arrayOfParameter.length == 5)
{
strtotal = arrayOfParameter[4];
}
else
{
strtotal = "";
}
Class<?> classOfNumber = NumberTypeOfString(arrayOfParameter[2]);
String strResult = "";
/**
* 1. Generate AP or OP by the total number of progression
*/
if( !strtotal.equals("") && strlast.equals("") )
{
for(int i = 0; i < Integer.valueOf(strtotal); i++)
{
switch(caculator.valueOf(choice))
{
case diff:
strResult += ( first + step * (i) )+ "\n";
break;
case ratio:
strResult += ( first * java.lang.Math.pow(step, (i))) + "\n";
break;
}
}
}
/**
* 2. Generate AP or OP by the last item
*/
else if( strtotal.equals("") && !strlast.equals("") )
{
int locationFirst = 0;
int locationLast = (int)(Double.valueOf(strlast) -first) / (int)step + 1;
for(int i = 0; i < locationLast - locationFirst; i++)
{
switch(caculator.valueOf(choice))
{
case diff:
strResult += ( first + step * (i) )+ "\n";
break;
case ratio:
strResult += ( first * java.lang.Math.pow(step, (i))) + "\n";
break;
}
}
}
/**
* 3. Generate AP or OP by the last item or by the total number of progression
*/
else if( !strtotal.equals("") && !strlast.equals("") )
{
int locationFirst = 0;
int locationLast = (int)(Double.valueOf(strlast) -first) / (int)step + 1;
int locationTotal = Integer.valueOf(strtotal);
for(int i = 0; i < ((locationLast - locationFirst)>locationTotal? (locationLast - locationFirst):locationTotal); i++)
{
switch(caculator.valueOf(choice))
{
case diff:
strResult += ( first + step * (i) )+ "\n";
break;
case ratio:
strResult += ( first * java.lang.Math.pow(step, (i))) + "\n";
break;
}
}
}
else
{
System.out.println("Wrong!!");
}
System.out.println("classOfNumber="+classOfNumber);
String[] arrayOfResult = strResult.split("\n");
ArrayList<T> resultList = new ArrayList<T>();
for(int i = 0; i < arrayOfResult.length; i++)
{
@SuppressWarnings("unchecked")
T elem = (T) arrayOfResult[i];
resultList.add( elem );
}
return resultList;
}
然而, 我使用以下ArrayList进行测试,没有问题:
ArrayList<Double> alst = new ArrayList<Double>();
alst.add(1.11);
alst.add(2.22);
alst.add(3.33);
Double[] DresultArray = listToArray(alst);
当我使用它时:
ArrayList<Double> arrayOfResult = createArrayOfProgression("diff; 1.11; 1.11; ;3");
//which has the same content: [1.11, 2.22, 3.33],
Double[] DresultArray = listToArray(arrayOfResult);
跳出另一个异常:
java.lang.ClassCastException: [Ljava.lang.String; cannot be cast to [Ljava.lang.Double;
我不知道原因......
答案 0 :(得分:2)
Arrays.asList() returns an ArrayList which extends AbstractList and NOT ArrayList!
因此你的例外java.lang.ClassCastException: [Ljava.lang.String; cannot be cast to [Ljava.lang.Double
因此,如果你使用java.util.ArrayList
作为基本类型而不是java.util.List
,那么请将代码更改为下面的代码。
public class G {
public static <T> T[] listToArray(ArrayList<T> inputList)
{
@SuppressWarnings("unchecked")
Class<T> type = (Class<T>)inputList.get(0).getClass();
@SuppressWarnings("unchecked")
T[] resultArray = (T[]) Array.newInstance(type, inputList.size());
for(int i=0; i < inputList.size(); i++)
{
resultArray[i] = (T)inputList.get(i);
}
return resultArray;
}
public static void main( String[] args ) {
ArrayList<Double> lst = new ArrayList( Arrays.asList( new Double[]{1.11, 2.22, 3.33} ) );
Double[] d = listToArray( lst );
System.out.println(d[0]);
}
}
答案 1 :(得分:1)
改变这个:
Class<T> type = (Class<T>)NumberTypeOfString(inputList.get(0).toString());
对此:
Class<T> type = (Class<T>)(inputList.get(0).getClass());
来自Array.newInstance
doc:
componentType - 表示组件类型的Class对象 新数组
因此新数组的类型必须是List
中元素的类型。