MOOC_ Students selected Courses

Source: Internet
Author: User
Tags addall set set

Use a set set to hold the student's chosen course
Interface cannot be instantiated, so it is instantiated with its implementation class

The set interface cannot be instantiated and instantiated by hashset this implementation class, which requires importing the package
This.courses=new HashSet ();

List interface ArrayList implementation class

This.coursetoselect=new ArrayList (); To ensure that the container can be instantiated.

Arraylist.get (int index) It is important to note that an element inserted with a DD () or AddAll () method will always be of type object, and the Get () method will also be the object type, which is the element to take out. To do the corresponding type of strong turn, see the geographical procedures.
Arraylist.add (int index) inserts an element at the specified position, which must be less than the length of the current sequence table, and when it is equal to its length, it is inserted at the end of the array table
Arraylist.addall (int index, List L) inserts a sequence table at the specified position.

Arraylist.addall (list L) inserts a list sequence at the end of an existing ArrayList

Convert the array to ArrayList:
1.element[] A = {New Object 1,new object 2 ...} Create
The class where the 2.main method is located. AddAll (Array.aslist (a));//Add
3. Each array element is converted sequentially to the specified object type//conversion

The AddAll RemoveAll of the list class is the operation of the collection, using the Arrays.aslist (array) method

There are several ways to add elements to the list:
New List class
Public List Coursetoselect;
Initializing the list class
Public Listtest ()
{
This.coursetoselect=new ArrayList ();
}
Adding data to the list
Method_1
Coursetoselect.add (object 1);
Method_2
Coursetoselect.add (position, object 2);
Method_3
Coursetoselect.addall (Arrays.aslist (Array _ object 3));
Method_4
Coursetoselect.addall (position, Arrays.aslist (Array _ object 4));
Get
Course temp= (Course) coursetoselect.get (location);

Both AddAll and RemoveAll are set operations, so you need to convert the array into a collection.

Course Enquiry

/*
* Iterate through list with iterators
* @param args
*/
public void Testiterator ()
{
Iterator It=coursetoselect.iterator ();
while (It.hasnext ())
{
Course cr= (Course) it.next ();
System.out.println ("Course: +cr.id+": "+cr.name");
}
}
/*
* Access the collection elements with the For each method
*/
public void Testforeach ()
{
for (Object Obj:coursetoselect)
{
Course cr= (Course) obj;
System.out.println ("Course: +cr.id+": "+cr.name");
}
}
public static void Main (string[] args)
{
Listtest it=new listtest ();
It.testadd ();
It.testget ();
It.testiterator ();

It.testforeach ();
}

Course changes

public void Testmodify ()
{
Coursetoselect.set (4, New Course ("6", "Mao"));
}

Course Removal

public void Testremove ()
{
Course cr= (Course) Coursetoselect.get (4);
Coursetoselect.remove (CR);
Delete an element on a 4,5 location
Course[] courses={(Course) Coursetoselect.get (4), (Course) Coursetoselect.get (5)};
Coursetoselect.removeall (Arrays.aslist (courses));
into a set
Testforeach ();
}

Apply a generic management course

The elements in the collection, which can be any type of object (a reference to an object), generics, a collection of elements of a particular type, type checking during compilation, and the collection element can be obtained directly by the specified type

Only generics and their subclass objects can be added to a generic collection
A generic cannot be a base type, it must use its corresponding wrapper class
Format:

List<integer> a = new arraylist<integer> ();
A.add (1);
System.out.println ("+a.get (0)");

When a subclass inherits a parent class, it constructs a parameterless constructor by default, but if a constructor without a parameter in the parent class is an error, you need to define an argument-free constructor in the parent class.

--------------------------------------------------------------------------------------------------------------- --------------------------------------------

public String ID;
public String name;
Actually should be private, then use Get,set
Constructor with parameter
Public Course (String id,string name) {
This.id=id;
This.name=name;
}
Student.java
public String ID;
public String name;
public Set courses;
Introducing Set Packages
Provide id,name can construct student
Constructors
Public Student (String id,string name) {
This.id=id;
This.name=name;
This.courses=new HashSet ();
}
Courses is instantiated with HashSet because the set interface cannot be instantiated
Storing alternative courses with the list container
Listtest.java
public class listtest{
Public list coursestoselect;//introduces the properties of the list package//list type under Java.util
Public Listtest () {//constructor method used to initialize Coursestoselect property
This.coursestoselect=new ArrayList ();//introduction of ArrayList Package
}
Add a course to a container, call the method in list
public void Testadd () {
Course cr1=new Course ("1", "Data Structure");//constructor called Course
Coursetoselect.add (CR1);
Course temp=coursestoselect.get (0);
Error: Object is stored in the collection will become object type, need to do type conversion when removed
Course temp= (Course) coursetoselect.get (0);
System.out.println (Temp.id+temp.name);
Another method of overloading in ToDoList, adding a new element, and optionally specifying a location

}
public static void Main () {
Listtest lt=new listtest ();
Lt.testadd ();//run Java running mode
}
}
Todo
Course cr2=new Course ("2", "C language");
Coursestoselect.add (0,CR2);
Course temp2= (Course) coursetoselect.get (0);
System.out.println (Temp2.id+temp2.name);

The elements in the list are repeatable:
Coursestoselect.add (CR1);
Course temp0= (Course) coursestoselect.get (2);
System.out.println (Temp0.id+temp0.name);


Course cr3=new Course ("2", "C + +");
Coursestoselect.add (4,CR3);
Exception: subscript is out of bounds
Course[] course={new Course ("3", "discrete"), New Course ("4", "Assembly")};
Coursestoselect.addall (Arrays.aslist (course)); instance of//collection//convert array to List
Course temp3= (Course) coursetoselect.get (2);
Course temp4= (Course) Coursetoselect.get (3);
System.out.println (Temp3.id+temp3.name);

Course[] Course2={new Course ("5", "high Number"), New Course ("6", "English")};
Coursestoselect.addall (2,arrays.aslist (COURSE2));
Course temp5= (Course) coursetoselect.get (2);
Course temp6= (Course) Coursetoselect.get (3);
}
public void Testget () {
int size= coursestoselect.size (); Gets the length of the list by size ()
System.out.println ("Course to be selected:");
for (int i=0;i<size;i++) {
Course cr= (Course) coursestoselect.get (i);//Get the element at the corresponding position
System.out.println ("Course: +cr.id+": "Cr.name");
}
}
Call Lt.testget () in main;
The elements in the list are repeatable: ...

Iterate through the list//by iterating through the iterator method of the collection to get an instance of the iterator
public void Testiterator () {//iterator interface//import Package
Iterator It=coursestoselect.iterator ();
System.out.println ("The course to be selected with iterators:");
while (It.hasnext) {//If there are elements that are true
Course cr= (Course) it.next ();//Take element
System.out.println ("Course: +cr.id+": "Cr.name");
}
}
Call Lt.testiterator () in main;
Iterators depend on collections and exist

To access the collection elements through for each ()
public void Testforeach () {
System.out.println ("The course to be selected with for each:");
for (Object Obj:coursestoselect) {
System.out.println ("Course: +cr.id+": "Cr.name");
}
}


Course changes
public void Testmodify () {
Coursestoslect.set (4,new Course ("7", "Mao"));
}
Call It.testmodify () in Main, It.testforeach ();

Course Removal
public void Testremove ()
{
Course cr= (Course) Coursestoselect.get (4);//Get the element on the 4 position
System.out.println ("I am going to be removed from the course:" +cr.id+cr.name);
Coursestoselect.remove (CR);
System.out.println ("deleted course" +CR);
Coursestoselect.remove (4);
Testforeach ();

Delete an element on a 4,5 location
Course[] courses={(Course) Coursestoselect.get (4), (Course) Coursestoselect.get (5)};
Coursestoselect.removeall (Arrays.aslist (courses));//Convert to a set
Testforeach ();
}
Apply a generic management course
Cannot add a different type object to the list
The elements in the collection can be any type of object (a reference to an object)
Generics: Specifies that a collection can hold only certain types of objects, that it is checked during compilation, and that the collection elements can be obtained directly by the specified type
New Testgeneric.java
List notation with generics:
/**
* List to store alternative courses
*/
public class Listtest {
Public List Coursestoselect;
Public Listtest ()
{
This.coursestoselect=new ArrayList ();
}
/**
* Add a course to the Coursetoselect container
*/
public void Testadd () {
Create a Course object, using the Add method
Course cr1=new Course ("1", "Data Structure");//Examples of course objects
Coursestoselect.add (CR1);
Course temp=coursetoselect.get (0);
Course temp= (Course) coursestoselect.get (0);
Objects are saved to the object type, and are cast as type when removed
System.out.println ("added Course" +temp.id+ ":" +temp.name);

Course cr2=new Course ("2", "C + +");
Coursestoselect.add (0, CR2);
Course temp2= (Course) coursestoselect.get (0);
System.out.println ("added Course" +temp2.id+ ":" +temp2.name);

The elements in the list are repeatable:
Coursestoselect.add (CR1);
Course temp0 = (Course) coursestoselect.get (2);
System.out.println (Temp0.id+temp0.name);

Course cr3=new Course ("3", "C");
Coursetoselect.add (4, CR3);
Report exception array subscript out of bounds add is inserted at a location that can be accessed

Create a course array add two elements
Course[] Course ={new Course ("3", "Discrete Mathematics"), New Course ("4", "Assembly Language")};
Coursestoselect.addall (Arrays.aslist (course));
AddAll is collection.
Course temp3= (Course) coursestoselect.get (2);
Course temp4= (Course) Coursestoselect.get (3);

Course[] Course2={new Course ("5", "Advanced mathematics"), New Course ("6", "Linear Algebra")};
Coursestoselect.addall (2, Arrays.aslist (COURSE2));
Course temp5= (Course) coursestoselect.get (2);
Course temp6= (Course) Coursestoselect.get (3);
}
/**
* Method of getting elements in list
*/
public void Testget ()
{
System.out.println ("Optional Course:");
for (int i=0;i<coursestoselect.size (); i++)
{
Course cr= (Course) coursestoselect.get (i);
System.out.println ("Course: +cr.id+": "+cr.name");
}
}
/**
* Iterate through list with iterators
* @param args
*/
public void Testiterator ()
{
Iterator It=coursestoselect.iterator ();
System.out.println ("The course to be selected with iterators:");
while (It.hasnext ())
{
Course cr= (Course) it.next ();
System.out.println ("Course: +cr.id+": "+cr.name");
}
}
/**
* Access the collection elements with the For each method
*/
public void Testforeach ()
{
System.out.println ("The course to be selected with for each:");
for (Object Obj:coursestoselect)
{
Course cr= (Course) obj;
System.out.println ("Course: +cr.id+": "+cr.name");
}
}
/**
* Modify the elements in the list
*/
public void Testmodify ()
{
Coursestoselect.set (4, New Course ("7", "Mao"));
}
/**
* Remove elements from list
* @param args
*/
public void Testremove ()
{
Course cr= (Course) Coursestoselect.get (4);//Get the element on the 4 position
System.out.println ("I am going to be removed from the course:" +cr.id+cr.name);
Coursestoselect.remove (CR);
System.out.println ("deleted course" +CR);
Coursestoselect.remove (4);
Testforeach ();

Delete an element on a 4,5 location
Course[] courses={(Course) Coursestoselect.get (4), (Course) Coursestoselect.get (5)};
Coursestoselect.removeall (Arrays.aslist (courses));//Convert to a set
Testforeach ();
}
public static void Main (string[] args)
{
Listtest it=new listtest ();
It.testadd ();
It.testget ();
It.testiterator ();
It.testforeach ();
It.testmodify (); It.testforeach ();
It.testremove ();
}
}
Subtype object instance of a generic type
New Childcourse.java Inheritance Course
Error, there is a parameter constructor in Course, the compiler does not add an implicit constructor for it, and the child class must call the parent class's implicit constructor, so add the parameterless constructor public Course () {} in Course.

Testing generic collections in Testgeneric Mind adding object instances of subtypes of a generic type
public void Testchild () {
Childcourse ccr=new childcourse ();
Ccr.id= "3";
Ccr.name= "I am a sub-type Course object instance";
Courses.add (CCR);
}
Call Tg.testchild () in main; Tg.testforeach ();

A qualified type in a generic collection cannot use the base data type, and can be qualified by using the wrapper class to allow the underlying data type to be deposited
Example: Int~integer long~long Boolean~boolean
public void Testbasictype ()
{
List<int> list =new arraylist<int> ();
List<integer> list =new arraylist<integer> ();
List.add (1);//is forced to be wrapped into an integer type
System.out.println ("Basic type must use wrapper class as generic" +list.get (0));

}


Manage student programs with set collections
Cannot modify the element at the specified position (unordered)

property of Set Type: Courses

Modify the public Set courses in Student.java;
Set<course> courses for public;
The This.courses=new hashset<course> () is added to the constructor;
New Settest.java
Public list<course> Coursestoselect;
Public Settest ()
{
Coursestoselect=new arraylist<course> ();
}
public void Testadd () {
Course cr1=new Course ("1", "Data Structure");//Examples of course objects
Coursestoselect.add (CR1);
Course temp= (Course) coursestoselect.get (0);

Course cr2=new Course ("2", "C + +");
Coursestoselect.add (0, CR2);
Course temp2= (Course) coursestoselect.get (0);

Course[] Course ={new Course ("3", "Discrete Mathematics"), New Course ("4", "Assembly Language")};
Coursestoselect.addall (Arrays.aslist (course));
Course temp3= (Course) coursestoselect.get (2);
Course temp4= (Course) Coursestoselect.get (3);
}
public void Testforeach ()
{
for (Object Obj:coursestoselect)
{
Course cr= (Course) obj;
System.out.println ("Course: +cr.id+": "+cr.name");
}
}
public static void Main (string[] args) {
Settest st=new settest ();
St.testadd ();
St.testforeach ();
Student objects
Student s=new Student ("1", "millet");
SYSTEM.OUT.PRINTLN ("Please choose Course" +s.name);

Build a Scanner object to receive the course ID entered from the keyboard
Scanner console=new Scanner (system.in);

for (int i=0;i<3;i++)
{
System.out.println ("Please enter the course ID");
String Courseid=console.next ();
for (Course Cr:st.coursesToSelect) {
if (Cr.id.equals (CourseID)) {
S.courses.add (CR);
Set retains the last one added, you can add null
}
}
}
St.testforeach (s);
}
public void Testforeach (Student s) {
for (Course cr:s.courses) {
System.out.println ("Selected course:" +cr.id+ ":" +cr.name);
}
}

MOOC_ Students selected Courses

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.