Java converts an array of objects to an int array

Java converts an array of objects to an int array … here is a solution to the problem.

Java converts an array of objects to an int array

What should I do? I have an object array ID and want to convert it to an int array

Solution

If Object[] objectArray like objectArray = {2,23,42,3} then

public static Integer[] convert(Object[] objectArray){
  Integer[] intArray = new Integer[objectArray.length];

for(int i=0; i<objectArray.length; i++){
   intArray[i] = (Integer) objectArray[i];
  }

return intArray;
 }

If your objectArray is similar to Object[] objectArray = new Integer[/*length*/];

You can simply convert (Integer []) objectArray;

Related Problems and Solutions