Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Object array to int array?

  1. #1
    Member
    Join Date
    Jul 2009
    Posts
    31
    Thanks
    3
    Thanked 6 Times in 5 Posts

    Default Object array to int array?

    ArrayList<Integer> in = new ArrayList<Integer>();
    //fill arraylist......
    int[] out;

    When using the ArrayList method toArray() it returns Object[], how would I go about converting this to int[]?

    out = in.toArray();

    Found java.lang.Object[] but expected int[]


    The most "obvious" solution i tried was a cast..but of course to no surprise i cant do that with an array.

    am i forced to pull each entry from the List using a loop?
    Last edited by rsala004; October 30th, 2009 at 12:59 AM.


  2. #2
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Object array to int array?

    Yes thats correct, you will come across some issues doing this, however you can convert your list of Integer into an array of Integer with no big problems, its just the matter of converting it to an array of primitives you will need to loop through the indexes I believe.

    Auto (un)boxing is only so good I'm afraid

            final List<Integer> intList = new ArrayList<Integer>();
            intList.add(4);
            intList.add(5);
            intList.add(6);
     
            final Integer[] intArray = intList.toArray(new Integer[intList.size()]);
     
            System.out.println(Arrays.toString(intArray));

    // Json

Similar Threads

  1. 2d Array
    By mgutierrez19 in forum Collections and Generics
    Replies: 1
    Last Post: October 24th, 2009, 10:48 PM
  2. 2D array help plz
    By Kilowog in forum Collections and Generics
    Replies: 2
    Last Post: October 11th, 2009, 03:23 PM
  3. Storing an array into an array
    By vluong in forum Collections and Generics
    Replies: 4
    Last Post: September 22nd, 2009, 02:14 PM
  4. help me to do this Array Program
    By fahdsaad in forum Member Introductions
    Replies: 1
    Last Post: June 30th, 2009, 02:19 AM
  5. Program of an Array in Java
    By eric_kapre in forum Collections and Generics
    Replies: 1
    Last Post: February 5th, 2009, 06:30 AM