-
March 23rd, 2010, 10:51 AM
#1
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.
-
March 23rd, 2010, 10:53 AM
#2
Re: ArrayList to int Array
------
If you are satisfied with the responses, add to the user's rep!
-
March 23rd, 2010, 11:13 AM
#3
Re: ArrayList to int Array
Originally Posted by Deliverance
.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.
-
March 23rd, 2010, 12:31 PM
#4
Re: ArrayList to int Array
Originally Posted by versacestl
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);
}
-
March 23rd, 2010, 06:08 PM
#5
Re: ArrayList to int Array
Originally Posted by versacestl
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 [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
-
January 25th, 2021, 01:02 PM
#6
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|