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