Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > ArrayList -> int[]

Reply
Thread Tools

ArrayList -> int[]

 
 
Marc Dzaebel
Guest
Posts: n/a
 
      02-15-2004
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


 
Reply With Quote
 
 
 
 
Chris Smith
Guest
Posts: n/a
 
      02-15-2004
Marc Dzaebel wrote:
> 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
 
Reply With Quote
 
 
 
 
Andrew Hobbs
Guest
Posts: n/a
 
      02-15-2004

"Marc Dzaebel" <(E-Mail Removed)> wrote in message
news:c0mg97$edd$00$(E-Mail Removed)-online.com...
> 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


************************************************** *******


> Thanks, Marc
>
>



 
Reply With Quote
 
Jim Moonves
Guest
Posts: n/a
 
      02-15-2004
"Andrew Hobbs" <(E-Mail Removed)> wrote in
news:402ecb18$0$1734$(E-Mail Removed) u:

> 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?
 
Reply With Quote
 
Bjorn Abelli
Guest
Posts: n/a
 
      02-15-2004

"Jim Moonves" <(E-Mail Removed)> skrev i meddelandet
news:Xns948FD4272BE23nonenonecom@216.168.3.44...
> "Andrew Hobbs" <(E-Mail Removed)> wrote in
> news:402ecb18$0$1734$(E-Mail Removed) u:
>
> > 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?


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


 
Reply With Quote
 
Marc Dzaebel
Guest
Posts: n/a
 
      02-15-2004
"Bjorn Abelli" <(E-Mail Removed)> schrieb im Newsbeitrag
news:c0mmg2$18gsda$(E-Mail Removed)-berlin.de...
> 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[i]=((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[i]=((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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Clear Arraylist vs new ArrayList Philipp Java 6 05-28-2008 09:07 PM
Does the clone() method of ArrayList<> make a copy of the objects in the ArrayList? xz Java 16 08-04-2007 10:33 PM
a class inherited from ArrayList, is saved to ViewState, why the type of the object read from ViewSate is not the class, but the parent, ArrayList leal ting ASP .Net 1 02-10-2004 07:45 PM
writeObject with ArrayList of ArrayList? Kaidi Java 4 01-03-2004 08:16 PM
Iterate through ArrayList using an another ArrayList Saravanan Rathinavelu ASP .Net 3 08-19-2003 07:03 AM



Advertisments