Time:10-12
CodePudding user response:
first arraylist The second arraylist The third use arraylist collections. Sort order The fourth hashmap
import Java. Util. *; Import the Java. Util. Map. Entry; Public class CollectionDemo {Public static void main (String [] args) {//TODO automatically generated method stub Student st1=new Student (); Student st2=new Student (); Student st3=new Student (); Student [] st={st1, st2, st3}; Int count=1; CollectionTest01 ct1=new CollectionTest01 (); CollectionTest02 ct2=new CollectionTest02 (); CollectionTest03 tc3=new CollectionTest03 (); CollectionTest04 ct4=new CollectionTest04 (); Scanner sc=new Scanner(System.in); System. The out. Println (" * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 1 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * "); for (int i=0; i <3; I++) {System. Out. Print (" please enter the first "+" student student id: "+ count); St [I] setId (sc), next ()); count++; } Ct1. Set01. Add (st1); Ct1. Set01. Add (st2); Ct1. Set01. Add (st3); For (Student Student: ct1 set01) {System. The out. Println (student. GetId ()); } System. The out. Println (" * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 2 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * "); Count=1; for (int i=0; i <3; I++) {System. The out. Print (" please enter the first "+ count +" student's name: "); St [I] elegantly-named setName (sc), next ()); count++; } Ct2. List02. Add (st1); Ct2. List02. Add (st2); Ct2. List02. Add (st3); For (Student Student: ct2. List02) {System. The out. Println (student. The getName ()); } System. The out. Println (" * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * number three * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * "); Count=1; for (int i=0; i <3; I++) {System. The out. Print (" please enter the first "+ count +" student score: "); St [I] setScores (sc) nextDouble ()); count++; } Tc3. List03. Add (st1); Tc3. List03. Add (st2); Tc3. List03. Add (st3); Tc3. List03. Sort (new Student ()); For (Student Student: tc3. List03) {System. The out. Println (student. GetScores ()); } System. The out. Println (" * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * the fourth question * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * "); Count=1; for (int i=0; i <3; I++) {System. Out. Print (" please enter the first "+" student student id: "+ count); St [I] setId (sc), next ()); System. The out. Print (" please enter the first "+ count +" student's name: "); St [I] elegantly-named setName (sc), next ()); Ct4. Map04. Put (st [I] getId (), st [I]); count++; } Set EntrySet=ct4. Map04. EntrySet (); For (Entry Et: entrySet) {System. The out. Print (" student id: "+ et. GetKey () +" "); System. The out. Println (" name: "+ et. GetValue (). The getName ()); } Sc. The close (); } } The class Student implements Comparator {private String id; private String name; Private double scores; Public String getId () {return id; } Public void setId (String id) {this.id=id; } Public String getName () {return name; } Public void elegantly-named setName (String name) {this.name=name; } Public double getScores () {Return scores; } Public void setScores (double scores) {This. Scores=scores; } @ Override Public int the compare (Student Student o1, o2) {//TODO automatically generated method stub Return compare1.c (o1. GetScores (), o2, getScores ()); } Public int compare1.c (Double o1, o2) Double {Return to o1.com pareTo (o2); } } The class CollectionTest01 {Public Set Set01; Public CollectionTest01 () {Set01=new HashSet (a); } } The class CollectionTest02 {Public List List02; Public CollectionTest02 () {List02=new ArrayList (a); } } The class CollectionTest03 {Public List List03; Public CollectionTest03 () {List03=new ArrayList (a); } } The class CollectionTest04 {Public Map Map04; Public CollectionTest04 () {Map04=new HashMap (a); } nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
Page link:https//www.codepudding.com/Backend/62547.html