• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Vector and Array please help

 
jay lai
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 257
Firefox Browser Hibernate Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15441
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15441
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic