A Vector is an object, so you can store it in another Vector. (You could store it in itself, too, but you might have trouble printing the list, for example.)
What if we have three students, each with up to five term marks? We need a list of students, with each item in the list owning a list of marks.
Vector s0 = new Vector(); // the first student's marks Vector s1 = new Vector(); // the second student's marks Vector s2 = new Vector(); // the third student's marks
Let's make a lecture section of students:
Vector course = new Vector(); course.addElement(s0); course.addElement(s1); course.addElement(s2);
Suppose the first student has three marks: 65, 89 and 47.
s0.addElement(new Integer(65)); s0.addElement(new Integer(89)); s0.addElement(new Integer(47)); ... repeat to add several marks for each student …
Let's retrieve the second student's fourth mark:
Vector stu = (Vector)course.elementAt(1); Integer mk = (Integer)stu.elementAt(3); int mark = mk.intValue();
Or we could have used one statement to do the same as the above three lines:
int mk = ((Integer) ((Vector) course.elementAt(1)). elementAt(3)).intValue();
[ CSC108 Home | Outline | Announcements | Newsgroup | Assignments | Lectures | Marks | Links ]
© Copyright 2000. All Rights Reserved.