wood burning stoves 2.0*
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
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
Author

Vector of string arrays contains same values

Gary Frick
Greenhorn

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 = {"",""};
while(it.hasNext())
{
Map.Entry em = (Map.Entry)it.next();
rowData[0] = em.getKey().toString();
System.out.println(em.getKey());
rowData[1] = em.getValue().toString();
System.out.println(em.getValue());
data.addElement(rowData);
}
}
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:
2001
2
2002
4
2003
1
2004
Appreciatively,
Gary
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 986

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
Greenhorn

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,
Gary
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Vector of string arrays contains same values