aspose file tools*
The moose likes Beginning Java and the fly likes Vector and Array please help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Vector and Array please help" Watch "Vector and Array please help" New topic
Author

Vector and Array please help

jay lai
Ranch Hand

Joined: Apr 04, 2002
Posts: 180
Hello there,
I have a vector, and Array size of 4
I loops through the vector.size() and I need assigned the value of each element in the array to the value in the vector.
String [] arr = new String[4];
for (int x=0;x<myVector.size();x++)
{
what do i do here? thanks
}

The scenario is: on the web application, allow to add multi user, and each user allow to enter their id1,id2,id3,id4. I need to collect info of user1 associated with 4 ids, user2 with 4 ids...so on...Right now user is a Vector, and String [] ids = new String[4];

Please giving some help or thought, Many thanks in advance

Kashyap Hosdurga
Ranch Hand

Joined: Sep 19, 2005
Posts: 90
Jay,
You may have to use Map structure to map user and his 4 IDs.
Say User1 - ID1, ID2, ID3, ID4
User2 - ID5, ID6, ID7, ID8

If thi is the case then you can add the Map to a vector using
addAll(int index, Collection c)
Please correct me if I am wrong.
Srinivas Kalvala
Ranch Hand

Joined: Oct 20, 2005
Posts: 257

Hi,

String [] arr = new String[4];
for (int x=0;x<myVector.size();x++)
{
arr[x]=new String();
arr[x]=(String)myVector.get(x);
}

Thank you.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14337
    
  22

Srinivas, the first of your two lines does not make sense:

Why are you first creating a new String, and immediately after that you throw away the String and assign some other value to arr[x]? Just leave out the first of these two lines.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14337
    
  22

Originally posted by jay lai:
Hello there,
I have a vector, and Array size of 4
I loops through the vector.size() and I need assigned the value of each element in the array to the value in the vector.

What do you want - do you want to copy the content of the *array* into the *vector* or is it the other way around? In your sentence here you are saying array to vector, but your source code suggests vector to array.

Class Vector has a toArray() method, so you don't have to write the loop yourself. Unfortunately the code is a bit tricky if you want an array of a specific type (a String array, in your case):
Madhura Prakash
Greenhorn

Joined: May 18, 2006
Posts: 2
Hello,

Incase you want to copy the contents of the Vector to the Array you could also do it in the following manner,

Code:
----------------------------------------------------------------------------
String[] strCopiedFromVect = new String[vectorToBeCopied.size()];
vectorToBeCopied.copyInto(strCopiedFromVect);
----------------------------------------------------------------------------

Hope this helps..

Regards,
Madhura.
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780

These examples are in Java 5. The list returned by arrayToList1 is backed by the array. If that is not desired, the second version can be used.


There is no emoticon for what I am feeling!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Vector and Array please help