• Post Reply Bookmark Topic Watch Topic
  • New Topic

Vector of Arrays ( this should be an easy question)  RSS feed

 
Jimmy James
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vector<String[]> AffectedModels = new Vector<String[]>();
String[] ManAndModel = new String[2];
try{
System.out.println(SelectParameter);
ResultSet result = sql.executeQuery(SelectParameter);
while (result.next()){
ManAndModel[0] = result.getString("MANUFACTURER");
ManAndModel[1] = result.getString("MODEL");
System.out.println("Man:"+ManAndModel[0]+" MODEL:"+ManAndModel[1]);
AffectedModels.addElement(ManAndModel);
}
} catch (Exception f) {
f.printStackTrace();
}

int x=0;
while(x < AffectedModels.size()){
ManAndModel = new String[2];
ManAndModel = AffectedModels.elementAt(x);
System.out.println("2Man:"+ManAndModel[0]+" 2MODEL:"+ManAndModel[1]);
x++;
}

-------OUTPUT:--------
Man:Komatsu Forest MODEL:415EX
Man:Komatsu Forest MODEL:425EX
Man:Komatsu Forest MODEL:425EXL
Man:Komatsu Forest MODEL:445EX
Man:Komatsu Forest MODEL:445EXL
Man:Komatsu Forest MODEL:475EX
Man:Komatsu Forest MODEL:475EXL
Man:Komatsu Forest MODEL:941 Wheel Harvester
Man:Komatsu Forest MODEL:890.2 8 Wheel Loader
2Man:Komatsu Forest 2MODEL:890.2 8 Wheel Loader
2Man:Komatsu Forest 2MODEL:890.2 8 Wheel Loader
2Man:Komatsu Forest 2MODEL:890.2 8 Wheel Loader
2Man:Komatsu Forest 2MODEL:890.2 8 Wheel Loader
2Man:Komatsu Forest 2MODEL:890.2 8 Wheel Loader
2Man:Komatsu Forest 2MODEL:890.2 8 Wheel Loader
2Man:Komatsu Forest 2MODEL:890.2 8 Wheel Loader
2Man:Komatsu Forest 2MODEL:890.2 8 Wheel Loader
2Man:Komatsu Forest 2MODEL:890.2 8 Wheel Loader

My Question is, why does it only display the last item when I try to read out the items.
 
Joe Ess
Bartender
Posts: 9441
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are only creating one String array. All the entries in the Vector point to the same array.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jimmy,

Welcome to JavaRanch!

When you store an object in a Vector, a reference to that object is stored; the object is not copied. So if you write a loop whereby you store an object, change the object, store it again, change it again... then at the end, you'll have a Vector containing many references to the same object. If you look at any of those references, they all point to that single object, whose members contain the last set of values you set them to.

You want to create a new String[2] each time around your result.next() loop, so each time you're operating on a separate array.
 
Jimmy James
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ahh thanks guys, It appears that pointing vs copying has fooled me once again.
I fixed it with this:

AffectedModels.addElement(new String[]{result.getString("MANUFACTURER"),result.getString("MODEL")});

Thanks again!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!