0

I have an arraylist and I want to convert this array list to int[]. How can I do this? this is my function, this return arraylist

globalClass.allTrips.get(extras.getInt("position")).getItemsCostCategory();
Guy Rajwan
  • 33
  • 1
  • 10

2 Answers2

0

You can do this by allocating an int[] that is the same size as the input list, then copy values 1-by-1 from the input list to the result list, casting to int as you go.

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();
}
dana
  • 17,267
  • 6
  • 64
  • 88
0

try this:

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];
}

but remmember when you parse double to int , you will lose your decimal value

1.6 => 1

2.5 => 2

Developer
  • 460
  • 4
  • 17