vector addElement

K

korcs

Hi,

I have a problem with using a Vector.

I want to store the rows of an sql query resultset in a vector.

I add the values into it in a loop.

Here is my code:

while (SQLResultSet.next()) {

// buffering one row

for(int i=0; i<fields.length; i++) {

//the row is stored in a string array
row_container = SQLResultSet.getString(fields);
}

buffer.addElement(row_container); // adding the string array to
the vector

}

In the row_container I store in each iteration one resultset row.


The problem is, that it seems to me, that the addElement method adds
the row_container to the end of the vector and then changes the value
of all its previous instances.

So the content of the vector (after 1,2,3,4 loops) is like:

First loop:
1 | snake | reptile

Second loop:
2 | frog | amphibian
2 | frog | amphibian

Third loop:
3 | tuna | fish
3 | tuna | fish
3 | tuna | fish

Forth loop:
4 | racoon | mammal
4 | racoon | mammal
4 | racoon | mammal
4 | racoon | mammal

Instead of:

First loop:
1 | snake | reptile

Second loop:
1 | snake | reptile
2 | frog | amphibian

Third loop:
1 | snake | reptile
2 | frog | amphibian
3 | tuna | fish

Forth loop:
1 | snake | reptile
2 | frog | amphibian
3 | tuna | fish
4 | racoon | mammal

Could you tell me how could i get the latter result?

Thx.

Best,

korcs
 
R

Roedy Green

The problem is, that it seems to me, that the addElement method adds
the row_container to the end of the vector and then changes the value
of all its previous instances.

I think what you are disturbed about is when you write:

v1 = new Vector();
....
v2 = v1;

v2.addElement( x );

When you look at v1 it has x too.

This is because there is only one Vector object and both v1 and v2
point to it. This is a general Java object problem, not just Vectors.

To get round it you must make a copy of the Vector object, either
field by field into a new Vector or by using the clone() method. See
http://mindprod.com/jgloss/clone.html

By the way, Vector is almost never used these days, supplanted by
ArrayList.
 
L

Lasse Reichstein Nielsen

korcs said:
I want to store the rows of an sql query resultset in a vector.

I add the values into it in a loop.

Here is my code:

You are missing the declaration of row_container.
I assume it's
String[] row_container = new String[fields.length];
and is placed outside the while loop.
while (SQLResultSet.next()) { .... fill row_container ...
buffer.addElement(row_container); // adding the string array to the vector
In the row_container I store in each iteration one resultset row.
The problem is, that it seems to me, that the addElement method adds
the row_container to the end of the vector and then changes the value
of all its previous instances.

No. What happens is that you add the *same* array to the Vector multiple
times, and you update that array in your loop.

Try instead to move the declaration of row_container inside the while-loop.
This will ensure that you create a new array for each row.

/L
 
K

korcs

korcs said:
I want to store the rows of an sql query resultset in a vector.
I add the values into it in a loop.
Here is my code:

You are missing the declaration of row_container.
I assume it's
String[] row_container = new String[fields.length];
and is placed outside the while loop.
while (SQLResultSet.next()) {

... fill row_container ...
buffer.addElement(row_container); // adding the string array to the vector
In the row_container I store in each iteration one resultset row.
The problem is, that it seems to me, that the addElement method adds
the row_container to the end of the vector and then changes the value
of all its previous instances.

No. What happens is that you add the *same* array to the Vector multiple
times, and you update that array in your loop.

Try instead to move the declaration of row_container inside the while-loop.
This will ensure that you create a new array for each row.

It fixed the problem! Thx!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,159
Latest member
SweetCalmCBDGummies
Top