Use the object generic model and ArraysList to enter and display the student information. The arrayslist student

Source: Internet
Author: User

Use the object generic model and ArraysList to enter and display the student information. The arrayslist student

Question: Enter and save the student information. When the student number is 0, the student information is displayed.

// Student Class

Public class Student {

Public int id;

Public String name;

Public int age;

Public Student (){

Super ();

}

Public Student (int id, String name, int age ){

Super ();

This. id = id;

This. name = name;

This. age = age;

}

// Text

Public class Text {
Public static void main (String [] args ){
System. out. println ("***** enter the student information. When the student number is 0, the end is *****");
Wrote input = new partition (System. in );
ArrayList <Student> arraylist = new ArrayList <Student> ();
// Enter Student Information
While (true ){
Student student = new Student ();
System. out. print ("Enter the student ID :");
Student. id = input. nextInt ();
If (student. id = 0 ){
System. out. println ("End entry! ");
Break;
} Else {
System. out. print ("Enter the Student name :");
Student. name = input. next ();
System. out. print ("Enter the student age :");
Student. age = input. nextInt ();
Arraylist. add (student );
}
}

// Output Student Information
System. out. println ("Student Information :");
System. out. println ("student ID \ t name \ t age ");
Iterator <Student> iterator = arraylist. iterator ();
While (iterator. hasNext ()){
Student a = iterator. next ();
System. out. println (a. id + "\ t" + a. name + "\ t" + a. age );
}

}

Running result:

Related Article

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.