CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Mar 2007
    Posts
    144

    ArrayList to int Array

    I have an ArrayList that has elements i need to retrieve out of it via index postitions. I am trying to cast or get those elements into an int[] array..

    I have tried casting the arraylist into int[] and using .get(i) to retrieve the indices in a for loop.
    I have also tried using the .toArray method to push the elements over..


    I have something like this..

    Code:
       ArrayList List = new ArrayList();
       // List is passed integers via another class call
    
       int[] array = new int[List.size()];
    
       for(int i = 0; i < array.length; i++)
       {
             array[i] = (int[])List.get(i);     
       }
    Any help to get the elements in the ArrayList into an int[] array would be great.

    Thanks.

  2. #2
    Join Date
    Apr 2007
    Posts
    425

    Re: ArrayList to int Array

    .toArray(new Integer[0])
    ------
    If you are satisfied with the responses, add to the user's rep!

  3. #3
    Join Date
    Mar 2007
    Posts
    144

    Re: ArrayList to int Array

    Quote Originally Posted by Deliverance View Post
    .toArray(new Integer[0])
    I am getting an incompatible types declaration: finding Object[] and needs int[]

    I tried

    Code:
       array = List.toArray(new Integer[List.size()]);
    and

    Code:
       int[] array = List.toArray(new Integer[List.size()]);
    and

    Code:
       int[] array = List.toArray(new Integer[0]);

    but i figured it out.. This seems to work as i was using a cast (int[]) before

    Code:
      for(int i = 0; i < List.size(); i++)
                {
                    array[i] = ((Integer) List.get(i)).intValue();
                }
    Thanks for your quick response deliverance.

  4. #4
    Join Date
    May 2009
    Posts
    2,413

    Re: ArrayList to int Array

    Quote Originally Posted by versacestl View Post
    I am getting an incompatible types declaration: finding Object[] and needs int[]
    You need to cast because your code isn't typesafe. Look at this, no casting,
    Code:
    ArrayList<Integer> al = new ArrayList<Integer>(); // type declared ArrayList
    // 
    int[] ia = new int[al.size()];
    for (int i=0; i<ia.length; i++) {
       ia[i] = al.get(i);
    }

  5. #5
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: ArrayList to int Array

    Quote Originally Posted by versacestl View Post
    I am getting an incompatible types declaration: finding Object[] and needs int[]
    This is because an int[] is not an Object[] (or Integer[]). While autoboxing will transparently convert between int and Integer, it doesn't include arrays.

    A simple solution would be to use an Integer[]:
    Code:
    Integer[] array = List.toArray(new Integer[List.size()]);
    Unless you have to pass the array around as an int[] parameter or return value, autoboxing will take care of converting individual elements from Integer to int and vice versa whenever necessary.

    Testing can show the presence of errors, but not their absence...
    E. Dijkstra
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  6. #6
    Join Date
    Jan 2021
    Posts
    1

    Re: ArrayList to int Array

    We have below ways to convert ararylist to array

    1. Using public Object[] toArray() - it will return array of object and after that we need to convert it int

    Object[] array = list.toArray();

    2. public T[] toArray(T[] a) - In this way we will create array of integer and toArray Take it as argument then return it

    Integer[] arr = new Integer[list.size()];
    arr = list.toArray(arr);

    3. Public get() method - Iterate ararylist and one by one add element in array.

    For more details for these method Visit this article how to convert arraylist to array

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured