aspose file tools*
The moose likes Beginning Java and the fly likes Vectors are making me mad! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Vectors are making me mad!" Watch "Vectors are making me mad!" New topic
Author

Vectors are making me mad!

rich werth
Ranch Hand

Joined: Sep 21, 2001
Posts: 57
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
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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.


"I'm not back." - Bill Harding, Twister
rich werth
Ranch Hand

Joined: Sep 21, 2001
Posts: 57
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.
Doanh Nguyen
Ranch Hand

Joined: Dec 02, 2000
Posts: 45
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

Joined: Sep 21, 2001
Posts: 57
Thanks
Putting vector in the loop worked
Vectors aren't making me mad anymore.
May be tomorrow!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Vectors are making me mad!