my dog learned polymorphism*
The moose likes Java in General and the fly likes Vector of string arrays contains same values Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Vector of string arrays contains same values" Watch "Vector of string arrays contains same values" New topic

Vector of string arrays contains same values

Gary Frick

Joined: Sep 13, 2002
Posts: 28
In the following code, the vector of arrays, all contain the same value, yet as you can see, by the System.out display, there are definitely unique values in the Iterator. Why are all of the vector entries being overwritten by the last values inserted? I orginally tried this as loading a vector of vectors and that had problems too, so I moved to a vector of String arrays.
Vector data = new Vector();
public void loadTableData(Map BBtally)
Collection entries = BBtally.entrySet();
Iterator it = entries.iterator();
String[] rowData = {"",""};
Map.Entry em = (Map.Entry);
rowData[0] = em.getKey().toString();
rowData[1] = em.getValue().toString();
Values as they appear in Vector afterwards. As I step through debug I can see these values change to the last value loaded into rowData.
data = Vector
[0] =(string[])
[0] = "2004"
[1] = "1"
[1] =(string[])
[0] = "2004"
[1] = "1"
[2] =(string[])
[0] = "2004"
[1] = "1"
[3] =(string[])
[0] = "2004"
[1] = "1"

Values being displayed in System.out:
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 984

No idea.
But your output which show the data in the debug output is confusing to me. Are you sure you are looking at the results of the array?

Please ignore post, I have no idea what I am talking about.
Blake Minghelli
Ranch Hand

Joined: Sep 13, 2002
Posts: 331
If I understand what you're trying to do then I think I see your problem.
It looks like you are trying to fill a Vector with 2-element arrays right? And each array has the values from your map? If that's correct, then you need to create your array inside of the while(...) loop. Because you create your array outside of the loop, the code inside of the loop is just overwriting the values of the same array. In other words, each iteration just replaces rowData's elements. And you are adding references to the same array in your Vector. If you place the inside of your loop, then it will create a new array with each iteration.
Hope that helps.
P.S. - you should also consider using an ArrayList instead of a Vector for better performance

Blake Minghelli<br />SCWCD<br /> <br />"I'd put a quote here but I'm a non-conformist"
Gary Frick

Joined: Sep 13, 2002
Posts: 28
Yes, specifying a 'new' vector on each pass was the silver bullet. Apparently it was adding the same rowData vector that always had the same pointer under the covers. I always thought that each new vector element created a new instance which was not related to the source data used to create the element.
Much Appreciated,
subject: Vector of string arrays contains same values
Similar Threads
Problems printing a Treemap 2nd run
can't get JTable(vector, vector) constructor to work
Sorting array's
Data doesn't show until I resize frame or click on table
JTable ClassCastException...