We help IT Professionals succeed at work.

Converting an Object[] array into an Integer[] and int[] array

AttilaB
AttilaB asked
on
Hi,

I am really puzzled that when I convert an ArrayList into an Object[] array that I cannot convert
the Object[] I get to another data type such as the primitive int[] or the wrapper class instance
Integer[].

See attached code.
package convertarraylisttoarray;

import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.Scanner;

public class ConvertArraylistToArray {
    
    private static ArrayList<Integer> myArrayList = new ArrayList<Integer>(); 
    
    public static void main(String[] args) {
        
        Scanner input = new Scanner(System.in);
                        int num = 0;
                
        System.out.println("Enter non-numeric character to stop data entry.");        
            
                
                  try
                  
              {
                    while (true) {
                    System.out.println("Enter next number to be stored into the myArrayList:"); 
                    num = input.nextInt();
                    myArrayList.add(num);
                      //get input for each value in array, and store values into arraylist  
                    }
                }
                  catch (InputMismatchException e)  {
                      System.out.println("You have entered all the numbers");             
                  }
                  
         
        // toArray() method of ArrayList class will return an array of OBJECTS          
        Object[] objectArray = new Integer[myArrayList.size()];
        objectArray = myArrayList.toArray();
        
        // Printing out the value of objectArray elements:
        for (int i=0;i<objectArray.length; i++ )
                    System.out.println( String.valueOf(objectArray[i]));  
        
        // Converting to Integer[] array:
        Integer intArray[] = new Integer[myArrayList.size()];
        
        // Convert String  to Integer:
        // (   String.valueOf(objectArray[i])      will give you the stored values as String)
        for (int i=0; i< myArrayList.size(); i++ )
            intArray[i] = Integer.getInteger(String.valueOf(objectArray[i]));
        
        // Print all elements in Integer array:
        for (int i=0; i< intArray.length; i++ )
            System.out.println(intArray[i]);   
        
        // It will be all null elements in the Integer[] array.  Why?
        
        // What is the best way to convert from Object[] array to int[] or Integer[] array?
        
    } 
      
}

Open in new window

/home/user/Oracle/Middleware/jdk160_24/bin/java -client -classpath /home/user/jdeveloper/mywork/ConvertArraylistToArray/.adf:/home/user/jdeveloper/mywork/ConvertArraylistToArray/ConvertArraylistToArray/classes convertarraylisttoarray.ConvertArraylistToArray
Enter non-numeric character to stop data entry.
Enter next number to be stored into the myArrayList:
1
Enter next number to be stored into the myArrayList:
2
Enter next number to be stored into the myArrayList:
3
Enter next number to be stored into the myArrayList:
4
Enter next number to be stored into the myArrayList:
5
Enter next number to be stored into the myArrayList:
d
You have entered all the numbers
1
2
3
4
5
null
null
null
null
null
Process exited with exit code 0.

Open in new window

Comment
Watch Question

Awarded 2011
Awarded 2011
Commented:
int[] nums = new int[myArrayList.size()];
For (int j= 0; j<nums.length; j++){
nums[j] = (myArrayList.get(j)).intValue();
}
Awarded 2011
Awarded 2011

Commented:
Integer.getInteger(String.valueOf(objectArray[i]));

raed abput method Integer.getInteger(String)

http://docs.oracle.com/javase/6/docs/api/java/lang/Integer.html#getInteger%28java.lang.String%29

it returns intergere which is the value of system property which iis a string, not of a string repreesenting number 

Open in new window

Awarded 2011
Awarded 2011

Commented:
I replaced Integer.getInteger() with
Integere.parseInt(String)
and now it is working - see output below

package convertarraylisttoarray;

import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.Scanner;

public class ConvertArrayListToArray {

    private static ArrayList<Integer> myArrayList = new ArrayList<Integer>();

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
                        int num = 0;

        System.out.println("Enter non-numeric character to stop data entry.");


                  try

              {
                    while (true) {
                    System.out.println("Enter next number to be stored into the myArrayList:");
                    num = input.nextInt();
                    myArrayList.add(num);
                      //get input for each value in array, and store values into arraylist
                    }
                }
                  catch (InputMismatchException e)  {
                      System.out.println("You have entered all the numbers");
                  }


        // toArray() method of ArrayList class will return an array of OBJECTS
        Object[] objectArray = new Integer[myArrayList.size()];
        objectArray = myArrayList.toArray();

        // Printing out the value of objectArray elements:
        for (int i=0;i<objectArray.length; i++ )
                    System.out.println( String.valueOf(objectArray[i]));

        // Converting to Integer[] array:
        Integer intArray[] = new Integer[myArrayList.size()];

        // Convert String  to Integer:
        // (   String.valueOf(objectArray[i])      will give you the stored values as String)
        for (int i=0; i< myArrayList.size(); i++ )
            intArray[i] = Integer.parseInt(String.valueOf(objectArray[i]));

        // Print all elements in Integer array:
        for (int i=0; i< intArray.length; i++ )
            System.out.println(intArray[i]);

        // It will be all null elements in the Integer[] array.  Why?

        // What is the best way to convert from Object[] array to int[] or Integer[] array?

    }

}

Open in new window


Output:
Enter non-numeric character to stop data entry.
Enter next number to be stored into the myArrayList:
3
Enter next number to be stored into the myArrayList:
4
Enter next number to be stored into the myArrayList:
6
Enter next number to be stored into the myArrayList:
t
You have entered all the numbers
3
4
6
3
4
6

Open in new window

Author

Commented:
It works great, thank you very much.

as long as I change it to:

for (int j= 0; j<nums.length ; j++){   .........

(you accidentally typed For instead of for)

Author

Commented:
I went through the other ways you showed me in your posted code.

Everything works great, thanks for all your help.
Awarded 2011
Awarded 2011

Commented:
You are always welcome.

>(you accidentally typed For instead of for)

I was typing on the phone - it has this stupidity of capitalizing of the first letter on the line and sometimes no time to correct it