Win a copy of Java Persistence with Spring Data and Hibernate this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Tim Cooke
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Jeanne Boyarsky
Saloon Keepers:
  • Stephan van Hulst
  • Carey Brown
  • Tim Holloway
  • Piet Souris
Bartenders:

Vector and Array please help

 
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 257
Hibernate Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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):
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
I want my playground back. Here, I'll give you this tiny ad for it:
The Low Tech Laboratory Movie Kickstarter is LIVE NOW!
https://www.kickstarter.com/projects/paulwheaton/low-tech
reply
    Bookmark Topic Watch Topic
  • New Topic