Java Object array

Source: Internet
Author: User

Java Object array

From [email protected]

Declare the array with the class first;

Then assign the instance of the class to the array;

 PackageWorks;ImportJava.util.Scanner; Public classTest { Public Static voidMain (string[] args) {Manage M=NewManage ();           M.input ();           M.output ();    M.select_sort (); }     }classstudent{intSno//School NumberString sname;//name      intScore;//Achievements           voidSetDataintSno,string Sname,intscore) {           This. sno=Sno;  This. sname=sname;  This. score=score; }      voiddisplay () {System.out.println ("Student number" +sno+ "Student name" +sname+ "score" +score); }      intGetsno () {returnSno; } String sname () {returnsname; }      intGetscore () {returnscore; }  }classmanage{intNum=4; Student s[]=NewStudent[num];//declaring an Object array          voidinput () {System.out.println ("Enter student number, student name, student score"); Scanner input=NewScanner (system.in);  for(inti=0;i<num;i++) {S[i]=NewStudent ();//an instance of the class is assigned to an arrayS[i].setdata (Input.nextint (), Input.next (), Input.nextint ()); }              }     voidoutput () { for(inti=0;i<num;i++) S[i].display (); }     voidSelect_sort () {//from small to large          for(inti=0;i<num-1;i++)               for(intj=i+1;j<num;j++) {             if(s[i].sno>S[j].sno) {                 inttemp=S[i].sno; S[i].sno=S[j].sno; S[j].sno=temp; }         }          for(inti=0;i<num;i++) {s[i].display (); }     }     }
Enter student number, student name, student score 1003a1001002b1001009c1001000D  Student No. 1003 student name a score 100 student number 1002 student name B Grade 100 student number 1009 student name C Score 100 student number 1000 student name D Grade 100 student number 1000 student name a score 100 student number 1002 student name B Grade 100 student No. 1003 student name C Score 100 student number 1009 student name D score

Java Object array

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.