• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Vectors are making me mad!

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello
I have a vector where I am adding some data to but it doesn't let me do it the way i think it should work. What am i doing wrong. As you can see it doesn't let me add the second value to the first slot of the second vector. Item number one is added in stead.
The code is below with the outPut.
try {
submitRechargeFormData submitData = new submitRechargeFormData();
String splitSizeString = (String) session.getAttribute("splitSize");
int splitSize = Integer.parseInt(splitSizeString);
Vector addData = new Vector();
Vector data = new Vector();
for(int i = 1; i <= splitSize; i++) {
String reason = request.getParameter("reason" + i);
System.out.println("What is reason " + reason);
String entity = request.getParameter("entity" + i);
String costCenter = request.getParameter("costCenter" + i);
String location = request.getParameter("location" + i);
String account = request.getParameter("account" + i);
addData.addElement(reason);
System.out.println("Went into vector " + reason);
System.out.println("this is first element " + (String) addData.firstElement());
addData.addElement(entity);
addData.addElement(costCenter);
addData.addElement(location);
addData.addElement(account);
data.addElement(addData);
}
session.setAttribute("rechargeData", data);
String reason = (String)(((Vector)data.elementAt(0)).elementAt(0));
String reason2 = (String)(((Vector)data.elementAt(1)).elementAt(0));
System.out.println("first one : " + reason);
System.out.println("second one : " + reason2);
////////////////////////////////////////////
outPut
What is reason one
Went into vector one
this is first element one
What is reason two
Went into vector two
this is first element one
What is reason three
Went into vector three
this is first element one
first one : one
second one : one
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are only two Vectors created, ever, in your code. I suspect you want to create a new Vector for addData each time you go through the for loop. Since you didn't, all changes to addData are affecting the same object. And the "data" is a Vecotor containing multiple elements, but each element is a reference to the same single Vector represented by addData.
 
rich werth
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
I'm not sure I understand what you mean. I have a vector that I am adding strings to. Then i have a vector that (i think) i'm adding a vector to.
so i have a vector of vectors not a vector of elements. The vector of vectors should be the data vector?
Can you explain what you mean.
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by rich werth:
Hello
I have a vector where I am adding some data to but it doesn't let me do it the way i think it should work.


The first vector acts as a collection of fields, the second records. You might want to move the creation of the first vertor INSIDE the loop:


Vector data = new Vector();
for(int i = 1; i <= splitSize; i++) {
Vector addData = new Vector();
 
rich werth
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks
Putting vector in the loop worked
Vectors aren't making me mad anymore.
May be tomorrow!
 
The airline is called "Virgin"? Don't you want a plane to go all the way? This tiny ad will go all the way:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic