• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Vector looping

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why when i for loop through a vector, I get the same result back throughout the iteration, instead of the unique values?
thanks
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Post your code so we can see what you're doing wrong.
Junilu
 
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you sure that the int you're passing to the elementAt() method is the variable from your for loop?
Art
[ February 08, 2002: Message edited by: Art Metzer ]
 
shawn sandy
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Enumeration e = v.elements();
while ( e.hasMoreElements() ) {
golfer = (Golfer)e.nextElement();
System.out.println("HERE IS THE NAME FOR ==> " + golfer.getFirstName());
}
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Shawn
the coe you posted looks like it should work the way you want it to. If you are getting the same name over and over then the problem might be in the Vector and not in the Enumeration. Try printing right from the vector and see what the results are. If that is the problem then post the code that shows where you put stuff into the vector.
 
shawn sandy
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
here is the part of the code where I am loading the vector..

Golfer golfer = new Golfer();
golferList list = new golferList();
golfer.setFirstName("Shawn");
System.out.println("Golfer 1 => " + golfer.getFirstName());
list.addGolfer(golfer);
golfer.setFirstName("ted");
System.out.println("Golfer 1 => " + golfer.getFirstName());
//fileUtil.objectWrite(golfer);
//fileUtil.objectRead();
list.addGolfer(golfer);
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by shawn sandy:
Why when i for loop through a vector, I get the same result back throughout the iteration, instead of the unique values?
thanks


Based on the code you just posted showing how you build the list, you are probably getting the last name you added, right?
What's happening is that you are in fact using a single golfer. Your vector contains as many references to that same golfer as the number of times you invoke add().
You need to add a new golfer every time.
 
shawn sandy
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks! that was the trick
reply
    Bookmark Topic Watch Topic
  • New Topic