Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Vector of string arrays contains same values

 
Gary Frick
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 986
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Blake Minghelli
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Gary Frick
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic