• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Vector of string arrays contains same values

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1067
2
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
The only taste of success some people get is to take a bite out of you. Or this tiny ad:
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic