Java class set _ example: many-to-many relationship notes

Source: Internet
Author: User

Java class set _ example: many-to-many relationship notes

Instance requirements:

One student can select multiple courses and multiple students can participate in one course. This is a typical multi-to-many relationship.
To complete this program, we should first define two categories: Student for student information and course for course information. There is a collection in one student class to save all the courses, in the course, there must also be a class set that stores all the students.

3. Knowledge Used in this instance

1. List Set
2. Transfer references
3. iterator

4. Details

Course. Java

Import Java. util. list; import Java. util. arraylist; public class course {private string name; private int credit; private list <student> allstudents; Public Course () {This. allstudents = new arraylist <student> ();} public course (string name, int credit) {this (); this. name = Name; this. credit = credit;} public list <student> getallstudents () {return this. allstudents;} public void setname (string name) {This. name = Name;} public void setcredit (INT credit) {This. credit = credit;} Public String getname () {return this. name;} public ITN getcredit () {return this. credit;} Public String tostring () {return "Course name:" + this. name + "; course credits:" + this. credit ;}}

Student. Java

Import Java. util. list; import Java. util. arraylist; public class student {private string name; private int age; private list <course> allcourses; Public student () {This. allcourses = new arraylist <course> ();} public student (string name, int age) {this (); this. name = Name; this. age = age;} public list <course> getallcourses () {return this. allcourses;} public void setname (string name) {This. name = Name;} public void setage (INT age) {This. age = age;} Public String getname () {return this. name;} public int getage () {return this. age;} Public String tostring () {return "Student name:" + this. name + "; age:" + this. age ;}};

Import Java. util. iterator; public class testmore {public static void main (string ARGs []) {course C1 = New Course ("English", 3 ); // first course C2 = New Course ("computer", 5); // second course student S1 = new student ("James", 20 ); student S2 = new student ("Li Si", 21); Student S3 = new student ("Wang Wu", 22); Student S4 = new student ("Zhao Liu", 23 ); student S5 = new student ("Sun Qi", 24); Student S6 = new student ("Qian ba", 24 ); // The first course has three students participating in c1.getallstudents (). add (S1); c1.getallstudents (). add (S2); c1.getallstudents (). add (s6); s1.getallcourses (). add (C1); s2.getallcourses (). add (C1); s6.getallcourses (). add (C1); // The second course has six students attending c2.getallstudents (). add (S1); c2.getallstudents (). add (S2); c2.getallstudents (). add (S3); c2.getallstudents (). add (S4); c2.getallstudents (). add (S5); c2.getallstudents (). add (s6); s1.getallcourses (). add (C2); s2.getallcourses (). add (C2); s3.getallcourses (). add (C2); s4.getallcourses (). add (C2); s5.getallcourses (). add (C2); s6.getallcourses (). add (C2); // output the information of a course and observe how many students participate in the course \ system. out. println (C1); iterator <student> iter1 = c1.getallstudents (). iterator (); While (iter1.hasnext () {student s = iter1.next (); system. out. println ("\ t |-" + S);} // find the student's course system. out. println (s6); iterator <course> iter2 = s6.getallcourses (). iterator (); While (iter2.hasnext () {course c = iter2.next (); system. out. println ("\ t |-" + C );}}};

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.