Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

Vector to array?

 
 
wang
Guest
Posts: n/a
 
      10-19-2005
Hi all,
I've tried to use Vector.toArray() to copy the content of a vector
to an array without success. The vector contains only object of
the class MyClass. I've made the following attempts:

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

or:
arr = (MyClass[])myVector.toArray();

or:
arr = myVector.toArray(arr);

How should the method toArray() be used? Thanks in advance!

k.w.wang

 
Reply With Quote
 
 
 
 
Mat
Guest
Posts: n/a
 
      10-19-2005
wang wrote:
> Hi all,
> I've tried to use Vector.toArray() to copy the content of a vector
> to an array without success. The vector contains only object of
> the class MyClass. I've made the following attempts:
>
> MyClass[] arr = new MyClass[myVector.size()];
> arr = (MyClass)myVector.toArray();
>
> or:
> arr = (MyClass[])myVector.toArray();
>
> or:
> arr = myVector.toArray(arr);
>
> How should the method toArray() be used? Thanks in advance!
>
> k.w.wang
>

I don't know exactly why, but I use :

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


Mat
 
Reply With Quote
 
 
 
 
Massimo Dell'Andrea
Guest
Posts: n/a
 
      10-19-2005
Mat wrote:
> I don't know exactly why, but I use :
>
> MyClass[] arr;
> arr = (MyClass[])myVector.toArray(new MyClass[myVector.size()]);
>
>
> Mat


or
MyClass[] arr = (MyClass[])myVector.toArray(new MyClass[0]);

or
MyClass[] arr = (MyClass[])myVector.toArray(new MyClass[]{});

or
MyClass[] arr = new MyClass[myVector.size]);
myVector.toArray(arr);
 
Reply With Quote
 
Monique Y. Mudama
Guest
Posts: n/a
 
      10-19-2005
On 2005-10-19, wang penned:
> Hi all,
> I've tried to use Vector.toArray() to copy the content of a vector
> to an array without success. The vector contains only object of
> the class MyClass. I've made the following attempts:
>
> MyClass[] arr = new MyClass[myVector.size()];
> arr = (MyClass)myVector.toArray();
>
> or:
> arr = (MyClass[])myVector.toArray();
>
> or:
> arr = myVector.toArray(arr);
>
> How should the method toArray() be used? Thanks in advance!
>
> k.w.wang


This doesn't directly answer your question, but that new is
unnecessary. You're allocating memory, then instantly dropping your
only reference to it.

--
monique

Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
 
Reply With Quote
 
Babu Kalakrishnan
Guest
Posts: n/a
 
      10-19-2005
Massimo Dell'Andrea wrote:
> Mat wrote:
>
>> I don't know exactly why, but I use :
>>
>> MyClass[] arr;
>> arr = (MyClass[])myVector.toArray(new MyClass[myVector.size()]);
>>
>>
>> Mat

>
>
> or
> MyClass[] arr = (MyClass[])myVector.toArray(new MyClass[0]);
>
> or
> MyClass[] arr = (MyClass[])myVector.toArray(new MyClass[]{});
>
> or
> MyClass[] arr = new MyClass[myVector.size]);
> myVector.toArray(arr);


Solutions (1) and (2) are really not worth using because they
(unnecessarily) create an extra Array object. The third solution
(assuming that you correct it by replacing "size" with "size()") is
indeed the best solution - it's actually the same as what Mat proposed,
since the API guarantees that the argument passed will be returned with
the values filled in if the Collection will fit into it.

BK
 
Reply With Quote
 
Monique Y. Mudama
Guest
Posts: n/a
 
      10-20-2005
On 2005-10-20, Roedy Green penned:
> On Wed, 19 Oct 2005 17:24:01 -0600, "Monique Y. Mudama"
><(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.
>>So the new array is simply being

>
> I see what you mean now. It is not just he keyword "new" you would
> delete, but that whole line and just collapse it to:
>
> MyClass[] arr = (MyClass)myVector.toArray();
>
> This code won't work. The cast will fail since toArray without a model
> to follow, will produce an Object[].


Oh.

I thought this was a newbie "defensive new" thing. Instead, you're
saying that the quoted code works, and more importantly if you remove
the new, it doesn't?

Now I'm the one who's feeling like a newbie, because I find that to be
really bizarre. Granted, I've never played with toArray(), but ...

Well, I guess now I have to.

> You would have to write that as
>
> MyClass[] arr = (MyClass) myVector.toArray( new MyClass[
> myVector.size() ] );
>
> With generics you could drop the cast, and clever folk might prune it
> even further.
>
> What I think the OP meant to say was:
>
> MyClass[] arr = new MyClass[ myVector.size() ];
> arr = (MyClass) myVector.toArray( arr );
>
> or perhaps:
> MyClass[] arr = new MyClass[ myVector.size() ];
> myVector.toArray( arr );
>
> The first code will work even if the size is off a tad. The second
> will not.


--
monique

Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
 
Reply With Quote
 
Hemal Pandya
Guest
Posts: n/a
 
      10-20-2005

Roedy Green wrote:
> I see what you mean now. It is not just he keyword "new" you would
> delete, but that whole line and just collapse it to:
>
> 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?

 
Reply With Quote
 
Thomas Weidenfeller
Guest
Posts: n/a
 
      10-20-2005
Monique Y. Mudama wrote:
>>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.

There are basically two tricks to use toArray successfully:

1) Supply an array of the desired type. Otherwise, if you use the
version of the toArray() method with doesn't require an argument, you
get an Object[].

2) Supply a large enough array. Otherwise toArray() will allocate an
new array (but at least of the supplied type).

Ok, and of course, if you run in a multithreaded environment, you need
to take care of locking/protecting the Vector when you do this.

/Thomas
--
The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/...g/java/gui/faq
http://www.uni-giessen.de/faq/archiv....java.gui.faq/
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      10-20-2005
On Wed, 19 Oct 2005 23:55:58 -0600, "Monique Y. Mudama"
<(E-Mail Removed)> wrote or quoted :

>> MyClass[] arr = (MyClass)myVector.toArray();
>>
>> This code won't work. The cast will fail since toArray without a model
>> to follow, will produce an Object[].

>
>Oh.
>
>I thought this was a newbie "defensive new" thing. Instead, you're
>saying that the quoted code works, and more importantly if you remove
>the new, it doesn't?



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.

--
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.
 
Reply With Quote
 
Thomas Hawtin
Guest
Posts: n/a
 
      10-20-2005
Thomas Weidenfeller wrote:
>
> 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.


> Ok, and of course, if you run in a multithreaded environment, you need
> to take care of locking/protecting the Vector when you do this.


Not entirely. The worst that could happen, would be some nulls on the
end of the array.

The toArray is an atomic operation, so no need to worry about that. If
the Vector grows between finding it's size and toArray, then the array
gets reallocated. If it shrinks, you just get a few nulls at the end.
(The reference immediately after the end of the valid values is always
set to null, even if it wasn't before.)

Tom Hawtin
--
Unemployed English Java programmer
http://jroller.com/page/tackline/
 
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