ArrayList -> int[]

M

Marc Dzaebel

is there a *single* method for the above standard transformation?
arrayList.toArray() creates an Object[] array rather than an int[] array. Of
course a simple "for" loop solves the problem.

Thanks, Marc
 
C

Chris Smith

Marc said:
is there a *single* method for the above standard transformation?
arrayList.toArray() creates an Object[] array rather than an int[] array. Of
course a simple "for" loop solves the problem.

No.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
A

Andrew Hobbs

Marc Dzaebel said:
is there a *single* method for the above standard transformation?
arrayList.toArray() creates an Object[] array rather than an int[] array. Of
course a simple "for" loop solves the problem.

No.

After all, there is no way for a method to know that an ArrayList only
contains objects which can be converted in some systematic way to an int.

Andrew


--
********************************************************
Andrew Hobbs PhD

MetaSense Pty Ltd - www.metasense.com.au
12 Ashover Grove
Carine W.A.
Australia 6020

61 8 9246 2026
metasens AntiSpam @iinet dot net dot au


*********************************************************
 
J

Jim Moonves

No.

After all, there is no way for a method to know that an ArrayList
only contains objects which can be converted in some systematic
way to an int.

Andrew

I wonder if 1.5 will change that as primitive to associated Object
conversions are transparent?
 
B

Bjorn Abelli

Jim Moonves said:
I wonder if 1.5 will change that as primitive to
associated Object conversions are transparent?

Not as your subject indicates, but it will help.

Lets assume you want to use an ArrayList to store numbers of type "int",
well that's not possible, but if you use the wrapper Integer to instantiate
the ArrayList, you can use them in a similar manner, e.g.:

ArrayList<Integer> array = new ArrayList<Integer>();

array.add(1);
array.add(3);

If you now want to turn them back to an array of "int:s", you still have to
use the Integer-type:

Integer[] vektor = (Integer[]) array.toArray(new Integer[0]);

Since the *object* returned by toArray is an object of *array-type*, there
can't be any "unboxing" to e.g. an int[]. However, this is not really
necessary in most cases, since you now can use an Integer-object "almost" as
it would have been an int:

int sum = 0;
for (Integer temp : vektor)
{
sum = sum + temp;
}

System.out.println(sum);

// Bjorn A
 
M

Marc Dzaebel

Bjorn Abelli said:
int sum = 0;
for (Integer temp : vektor)
{
sum = sum + temp;
}

Bjorn, these new 1.5 features are in fact valuable and timesaving. Hope
they're stable soon.

For completeness, currently the shortest method (<1.5) then is:

int[] ints=new int[arryList.size()];
for(int=0; i<ints.length; i++)
ints=((Integer)arrayList.get(i)).intValue();

However, a method like "toSimpleArray(Class)" for basic homogeneous
Collections is possible and could ease the work E.g.:

class HomogeneousArrayList extends ArrayList {
public Object toSimpleArray(Class elementType) {
if(elementType.equals(Integer.class)) {
int[]ints=new int[size()];
for(int i=0; i<ints.length; i++)
ints=((Integer)get(i)).intValue();
return ints;
}
// ... other basic types
return null;
}
}

The above method could be placed into AbstractCollection but might violate
safety considerations.

Thanks for your quality comments!

Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top