Re: vector addElement



korcs <konrad.lindner@xxxxxxx> writes:

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
--
Lasse Reichstein Nielsen - lrn@xxxxxxxxxx
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
.



Relevant Pages

  • Re: "Sorting" assignment
    ...     If clause-1 is a declaration, the scope of any variables it declares ... in that direction lies COBOL. ... >I don't declare anything inside of a loop is why. ...
    (comp.programming)
  • Re: vector addElement
    ... What happens is that you add the *same* array to the Vector multiple ... and you update that array in your loop. ... Try instead to move the declaration of row_container inside the while-loop. ...
    (comp.lang.java.programmer)
  • Re: About String
    ... Ada, where declarations are just as executable as statements. ... I think it does make sense because it clearly defines the scope ... anywhere outside the declaration part. ... for I in A'Range loop ...
    (comp.lang.ada)
  • Re: Newbies question
    ... for I in Elements'Range loop ... I ruled out the declaration of a local array within the block statement Copy_List, because I thought that the compiler would mandate a return statement to be at the top level of the function's body, which I wouldn't be able to do, since the declared array would be local to the block statement. ...
    (comp.lang.ada)
  • Re: static or not static? That is the question!
    ... anything else but a normal declaration that can accept ANY ... This loop would be run only ONCE in the program's lifetime. ... What is strange is to FORBID it, ... I can't imagine wanting to declare a static object in a for statement. ...
    (comp.std.c)