2017-04-05 72 views
0

我有一个数组列表,我想将此数组列表转换为int []。 我该怎么做? 这是我的功能,该返回的ArrayList如何将数组列表<double>转换为int []

globalClass.allTrips.get(extras.getInt("position")).getItemsCostCategory(); 
+0

到目前为止您尝试过什么?数组列表包含什么? –

+0

在'ArrayList'中使用'toArray()' - 这将返回一个'Integer []' –

+1

你可以在这里参考:http://stackoverflow.com/questions/960431/how-to-convert-listinteger-to- int-in-java –

回答

0

您可以通过分配int[],其大小与输入列表相同做到这一点,那么1 * 1在输入列表中的值复制到结果列表当你走时,你可以铸造到int

ArrayList<Double> values = new ArrayList<Double>(); 
values.add(1.0); 
values.add(2.0); 
values.add(3.0); 

int[] values2 = new int[values.size()]; 
for (int i = 0; i < values.size(); i++) { 
    values2[i] = values.get(i).intValue(); 
} 
+1

谢谢你,它的工作! –

0

试试这个:

ArrayList<double> arrList = new ArrayList<double>(); 
arrList.add(2.5); 
arrList.add(1.6); 
double[] doubleArr = arrList.toArray(new double[arrList.size()]); // double arrayList to double[] 
int[] intArr = new int[doubleArr.length]; // create int[] in the same size as the double[] 
for (int i = 0 ; i < intArr.length ; i++) // fill the int[] with the double[] values ' of course with parse to int[] 
{ 
    intArr[i] = (int)doubleArr[i]; 
} 

但是当你解析双为int remmember,你将失去你十进制值

1.6 => 1

2.5 => 2