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[i] = SQLResultSet.getString(fields[i]);
}
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
Roedy Green - 31 Jul 2007 12:59 GMT
>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.

Signature
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Lasse Reichstein Nielsen - 31 Jul 2007 12:59 GMT
> 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

Signature
Lasse Reichstein Nielsen - lrn@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
korcs - 31 Jul 2007 13:29 GMT
> > I want to store the rows of an sql query resultset in a vector.
>
[quoted text clipped - 22 lines]
> 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!