GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes Vector problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Vector problem" Watch "Vector problem" New topic
Author

Vector problem

bill dimitry
Greenhorn

Joined: May 27, 2008
Posts: 5
Hi guys,
I have problem with a Vector. I get 26 records from a DB and enter them into a Vector. I've checked the ResultSet values and they are OK.
I start printing values (from the first object) when the ResultSet reaches 3 (�First Print Name (Code)�). The values printed are from the current object (from row 3 up) not from the position 0 and up to 23.
When I try to print the values second time (�Second Print Name(Code)�) only one (the last record�s) values are printed 26 times (Vector�s size).
It looks like every time a new object(Course) is entered into the Vector it replaces all other objects in it.

Any idea?

Thanks,
Bill

CODE:

ResultSet rs = getRecords();
Course c = new Course();
Course cr = new Course();
Vector<Course> coll = new Vector<Course>();
String code = null;
String course = null;
String level = null;
try {
while (rs.next()) {
code =rs.getString("courseCode");
course = rs.getString("courseName");
level = rs.getString("courseLevel");
System.out.println("Get Cluster: " + course);
c.setCourseCode(code);
c.setCourseName(course);
c.setCourseLevel(level);
coll.addElement(c);
if((rs.getRow()-1)>2){
cr = coll.elementAt(rs.getRow()-3);
System.out.println("First Print Name: "+cr.getCourseName());
System.out.println("First Print Code: "+cr.getCourseCode());
}
}
} catch (SQLException ex) {
System.out.println(ex.toString());
}
Enumeration e = coll.elements();
Course c1 = new Course();
while (e.hasMoreElements()) {
c1 = (Course) e.nextElement();
System.out.println("Second Print Name: "+c1.getCourseName());
System.out.println("Second Print Code: "+c1.getCourseCode());
}
chen weiwei
Greenhorn

Joined: Mar 30, 2008
Posts: 2
HI!
You must new Course in while(){this...} try it ;
MY test code:

[ May 28, 2008: Message edited by: chen weiwei ]
Vikas Kapoor
Ranch Hand

Joined: Aug 16, 2007
Posts: 1374
Hello Bill and Chen,

Welcome to Javaranch.

Bill,Whenver you are posting some code then always enclose it with CODE tag.

Chen has pointed out correctly. If you don't create the object of Course class every time then there will be a multiple copy of same object into your vector.

Second,

I didn't get the use of this.



Do you want to print the values only or want to keep the track of those values by storing them somewhere? What is the use of cr(Course) object?
[ May 29, 2008: Message edited by: Vishal Pandya ]
bill dimitry
Greenhorn

Joined: May 27, 2008
Posts: 5
Thanks guys!
It works now.

Sorry for the CODE tag. I didn't know.
I'll use it next time.

Kind Regards,
Bill
Vikas Kapoor
Ranch Hand

Joined: Aug 16, 2007
Posts: 1374
Welcome Bill.

When i read your name Bill Dimitry , a hollywood character came in my mind. But i couldn't figured out it.

Hmm... Who's that?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39044
    
  23
I could only find one Bill Dimitry on Google-at JavaRanch.
Vikas Kapoor
Ranch Hand

Joined: Aug 16, 2007
Posts: 1374
Not Bill Dimitry actually .Only Dimitry!

I don't remember in which film i heard 'Dimitry'.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Vishal Pandya:
Not Bill Dimitry actually .Only Dimitry!

I don't remember in which film i heard 'Dimitry'.


http://www.imdb.com/find?s=char;q=dimitry


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42024
    
  64
<hijack>
The most memorable Dmitri to me is the Soviet Premier Kissoff in Dr. Strangelove or: How I Learned to Stop Worrying and Love the Bomb. Since he is neither seen nor heard, only addressed by that name by Peter Sellers on the telephone, he doesn't show up in IMDB, but Peter Sellers' telephone monologue does (search for "Kissoff"). A little excerpt:
I'm sorry, too, Dmitri... I'm very sorry... *All right*, you're sorrier than I am, but I am as sorry as well... I am as sorry as you are, Dmitri! Don't say that you're more sorry than I am, because I'm capable of being just as sorry as you are... So we're both sorry, all right?

<end-of-hijack>


Ping & DNS - my free Android networking tools app
Vikas Kapoor
Ranch Hand

Joined: Aug 16, 2007
Posts: 1374
Thanks Ilja and Ulf.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39044
    
  23
Excellent film, Dr Stangelove, but please, no more about Dmitri or I shall send us all off to Meaningless Drivel.
bill dimitry
Greenhorn

Joined: May 27, 2008
Posts: 5
Hi guys,
I'm glad that my 'controversial' username is causing the debate.

By the way, don't go to the Meaningless Drivel (what's that?) yet. I have more problems.
Check this post

Kind Regards,
Bill
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39044
    
  23
Originally posted by bill dimitry:
Hi guys,
I'm glad that my 'controversial' username is causing the debate.
Meaningless Drivel (what's that?).
I think people were overreacting about the name [ ], and as for what's Meaningless Drivel:



You don't want to know!!

It's actually where people have friendly chats about anything non-computer related.
 
GeeCON Prague 2014
 
subject: Vector problem