Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Vector to array?

Reply
Thread Tools

Vector to array?

 
 
Roedy Green
Guest
Posts: n/a
 
      10-20-2005
On 20 Oct 2005 00:21:21 -0700, "Hemal Pandya" <(E-Mail Removed)>
wrote or quoted :

>> MyClass[] arr = (MyClass)myVector.toArray();
>>
>> This code won't work.

>
>It won't even compile, it converts an Object to an Array. Or am I
>missing something?


The following analogous program will compile, but it fails with a
CastCastException on run.

import java.util.ArrayList;
public class CastExper
{
/**
* test harness
*
* @param args not used
*/
public static void main ( String[] args )
{
ArrayList a = new ArrayList(10);
a.add( "elephant" );
a.add( "beanbag" );
String[] things = (String[])a.toArray();
for ( String thing: things )
{
System.out.println( thing );
}
}
}

This is how you would do it with generics

import java.util.ArrayList;
public class CastExper
{
/**
* test harness
*
* @param args not used
*/
public static void main ( String[] args )
{
ArrayList<String> a = new ArrayList<String>(10);
a.add( "elephant" );
a.add( "beanbag" );
String[] things = a.toArray( new String[ a.size() ] );
for ( String thing: things )
{
System.out.println( thing );
}
}
}


The following code will not compile, even though you would think
toArray could, if it were written differently, might take advantage of
its knowledge that a was an ArrayList<String>. toArray() is defined
without any generics. Because of type erasure, that knowledge is
useless in determining the actual return type. The information is
not around at run time to instantiate the precisely correct type of
array.

String[] things = a.toArray();


--
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.
 
Reply With Quote
 
 
 
 
Hemal Pandya
Guest
Posts: n/a
 
      10-21-2005

Roedy Green wrote:
> On 20 Oct 2005 00:21:21 -0700, "Hemal Pandya" <(E-Mail Removed)>
> wrote or quoted :
>
> >> MyClass[] arr = (MyClass)myVector.toArray();
> >>
> >> This code won't work.

> >
> >It won't even compile, it converts an Object to an Array. Or am I
> >missing something?

>
> The following analogous program will compile,


Yes it will, because it does not have the erroneous conversion from
Object[] to Object.

>
> This is how you would do it with generics


You had earlier mentioned that the case is not required with generics,
but it wasn't clear to me how. Thank you for this explanation.

 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      10-21-2005
On 20 Oct 2005 20:15:25 -0700, "Hemal Pandya" <(E-Mail Removed)>
wrote or quoted :

>Yes it will, because it does not have the erroneous conversion from
>Object[] to Object.


What code are you referring to? It is quite legal to cast an Object[]
to Object.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      10-21-2005
On Thu, 20 Oct 2005 09:49:49 +0200, Thomas Weidenfeller
<(E-Mail Removed)> wrote or quoted :

>>>MyClass[] arr = new MyClass[myVector.size()];
>>>arr = (MyClass)myVector.toArray();

>>
>>
>> Without paying any attention to whether or not the desired result is
>> accomplished, it looks to me like the reference arr is being assigned
>> to a new array, and then immediately reassigned to a different array.

>
>Not necessarily.
>
>> So the new array is simply being allocated, then immediately lost to
>> eventually be snagged by the GC.
>>
>> Am I missing something?

>
>You are missing how Vector.toArray() works. If the Vector fits in the
>supplied array, it will *return a reference to the supplied array*!
>toArray() will only allocate a new array if the Vector doesn't fit.


I think you may have overlooked the fact that in that particular
example he failed to pass the array to toArray(). So allocation was
pointless.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.
 
Reply With Quote
 
wang
Guest
Posts: n/a
 
      10-21-2005
With your help I've solved the problem. Thank you all!

k.w.wang

 
Reply With Quote
 
Monique Y. Mudama
Guest
Posts: n/a
 
      10-21-2005
On 2005-10-20, Roedy Green penned:
> On Wed, 19 Oct 2005 23:55:58 -0600, "Monique Y. Mudama"
><(E-Mail Removed)> wrote or quoted :
>
>>> MyClass[] arr = (MyClass)myVector.toArray();
>>>


[snip]

> That code on the top line will fail. The problem is toArray not
> know the type of the thing on the right, so all it can do is produce
> an Object[] filled with MyClasses, which is a totally different
> animal that a MyClass[] filled with MyClasses. It can't be cast to
> MyClass[].
>
> If you give it a MyClass[] the right size to fill, or even an
> exemplar empty MyClass[] array, then it knows the desired type of
> array.



That strikes me as relying on a side effect to have the code function.
Isn't that bad design?

--
monique

Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      10-22-2005
On Fri, 21 Oct 2005 10:01:20 -0600, "Monique Y. Mudama"
<(E-Mail Removed)> wrote or quoted :

>> If you give it a MyClass[] the right size to fill, or even an
>> exemplar empty MyClass[] array, then it knows the desired type of
>> array.

>
>
>That strikes me as relying on a side effect to have the code function.
>Isn't that bad design?


The term side effect usually refers to a method changing something not
one of the inputs or outputs. What do you mean by that term?
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.
 
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
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
Initializing vector<vector<int> > and other vector questions... pmatos C++ 6 04-26-2007 05:39 PM
Free memory allocate by a STL vector, vector of vector, map of vector Allerdyce.John@gmail.com C++ 8 02-18-2006 12:48 AM
how the vector is created, how to pass vector to webservices method apachesoap:Vector Rushikesh Joshi Perl Misc 0 07-10-2004 01:04 PM
how do i create a vector within a vector ? learningjava Java 5 10-17-2003 10:19 PM



Advertisments