Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / July 2007

Tip: Looking for answers? Try searching our database.

vector addElement

Thread view: 
korcs - 31 Jul 2007 12:51 GMT
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!


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.