問題:某班30個學(xué)生的學(xué)號為20070301-20070330,全部選修了Java程序設(shè)計課程,給出所有同學(xué)的成績(可用隨機數(shù)產(chǎn)生,范圍60-100),請編寫程序?qū)⒈景喔魑煌瑢W(xué)的成績按照從低到高排序打印輸出。
要求:分別用List、Map、Set來實現(xiàn),打印的信息包括學(xué)號、姓名和成績。
1、使用List集合來實現(xiàn)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.Iterator; import java.util.LinkedList; import java.util.TreeMap; public class Test2{ public static void main(String[] args){ /* 此處用ArrayList實現(xiàn) * * ArrayList<Student>al=new ArrayList<Student>(); for(int i=20070301,j=10;i<=20070330;i++,j++) { al.add(new Student(i,(int) (40*Math.random()+60), "同學(xué)"+j)); } //ArrayList排序借助Collections中的sort()方法實現(xiàn)。 Collections.sort(al, new Sortbygrade()); for(Student sd:al) System.out.println(sd); */ LinkedList<Student> lt= new LinkedList<Student>(); for ( int i= 20070301 ,j= 10 ;i<= 20070330 ;i++,j++) { lt.add( new Student(i,( int ) ( 40 *Math.random()+ 60 ), "同學(xué)" +j)); } //對鏈表排序 Collections.sort(lt, new Sortbygrade()); //輸出鏈表 for (Student sd:lt) System.out.println(sd); } } //學(xué)生類 class Student{ int num,grade; String name; //構(gòu)造函數(shù) public Student( int num, int grade,String name){ this .num=num; this .name=name; this .grade=grade; } //此處必須覆寫 public String toString(){ // System.out.println("hi"); return "學(xué)號:" + this .num+ "\t" + "姓名:" + this .name+ " " + "成績:" + this .grade; } } //創(chuàng)建一個比較器類 class Sortbygrade implements Comparator<Student>{ @Override public int compare(Student s1, Student s2) { if (s1.grade>s2.grade) return 1 ; if (s1.grade<s2.grade) return - 1 ; if (s1.grade==s2.grade) return s1.name.compareTo(s2.name); return 0 ; } } |
輸出結(jié)果如圖:
對List集合框架的總結(jié):
1、List集合其實是一個動態(tài)的數(shù)組,元素可以直接通過for循環(huán)取出,而不需要迭代。
2、輸出List集合時,會默認調(diào)用集合中存儲對象的toString()方法,所以在類中需要進行覆寫。
若不覆寫toString( )方法,則必須使用
1
2
3
4
5
|
for ( int i= 0 ;i<lt.size();i++) { Student s=lt.get(i); System.out.println( "學(xué)號:" +s.num+ " 姓名:" +s.name+ " 成績:" +s.grade); } |
3、List集合的排序需要借助于Collections工具類,即Collections.Sort(list,new 比較器類())方法。所以需要自定義一個比較器類,定義自己的比較規(guī)則。
2、使用Set集合來實現(xiàn)
(1)使用TreeSet來實現(xiàn)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
package com.package1; import java.util.*; public class StuScore { public static void main(String[] args) { TreeSet<Student> ts= new TreeSet<Student>( new Com()); //添加元素進去 for ( int i= 20070301 ,j= 1 ;i<= 20070330 ;i++,j++) { ts.add( new Student(i, "同學(xué)" +j,( int ) ( 40 *Math.random()+ 60 ))); } //迭代循環(huán)取出 Iterator<Student> it=ts.iterator(); while (it.hasNext()) { Student o1=it.next(); System.out.println( "學(xué)號:" +o1.num+ " " + "姓名:" +o1.name+ " " + " " + "成績:" +o1.grade); } } } //學(xué)生類 class Student { int num; int grade; String name; public Student( int num, String name, int grade) { this .num=num; this .name=name; this .grade=grade; } } class Com implements Comparator { @Override public int compare(Object o1, Object o2) { Student s1=(Student) o1; Student s2=(Student) o2; if (s1.grade>s2.grade) return 1 ; if (s1.grade<s2.grade) return - 1 ; if (s1.grade==s2.grade) { return new Integer(s1.num).compareTo( new Integer(s2.num)); } return 0 ; } } |
輸出結(jié)果為:
學(xué)號:20070307 姓名:同學(xué)16 成績:60
學(xué)號:20070309 姓名:同學(xué)18 成績:60
學(xué)號:20070314 姓名:同學(xué)23 成績:61
學(xué)號:20070318 姓名:同學(xué)27 成績:61
學(xué)號:20070322 姓名:同學(xué)31 成績:61
學(xué)號:20070306 姓名:同學(xué)15 成績:62
學(xué)號:20070310 姓名:同學(xué)19 成績:64
學(xué)號:20070302 姓名:同學(xué)11 成績:66
學(xué)號:20070308 姓名:同學(xué)17 成績:68
學(xué)號:20070321 姓名:同學(xué)30 成績:68
學(xué)號:20070330 姓名:同學(xué)39 成績:69
學(xué)號:20070303 姓名:同學(xué)12 成績:70
學(xué)號:20070320 姓名:同學(xué)29 成績:70
學(xué)號:20070323 姓名:同學(xué)32 成績:77
學(xué)號:20070313 姓名:同學(xué)22 成績:78
學(xué)號:20070304 姓名:同學(xué)13 成績:79
學(xué)號:20070324 姓名:同學(xué)33 成績:83
學(xué)號:20070326 姓名:同學(xué)35 成績:84
學(xué)號:20070327 姓名:同學(xué)36 成績:85
學(xué)號:20070311 姓名:同學(xué)20 成績:88
學(xué)號:20070305 姓名:同學(xué)14 成績:89
學(xué)號:20070329 姓名:同學(xué)38 成績:89
學(xué)號:20070316 姓名:同學(xué)25 成績:90
學(xué)號:20070301 姓名:同學(xué)10 成績:95
學(xué)號:20070312 姓名:同學(xué)21 成績:96
學(xué)號:20070317 姓名:同學(xué)26 成績:97
學(xué)號:20070319 姓名:同學(xué)28 成績:97
學(xué)號:20070325 姓名:同學(xué)34 成績:98
學(xué)號:20070315 姓名:同學(xué)24 成績:99
學(xué)號:20070328 姓名:同學(xué)37 成績:99
對TreeSet的總結(jié):
1、元素不可以重復(fù),而且TreeSet是有序的。
2、兩種排序方法:
(1)自定義一個比較器類,比如class Com implementsComparator{ } ,實現(xiàn)compare(Object o1, Object o2)方法,在其中定義比較規(guī)則。
(2)讓元素自身具備比較性。
步驟:將add進TreeSet中的元素實現(xiàn)Comparable接口,并且覆蓋compareTo方法。這種順序也是元素的自然順序,或者叫做默認順序。
方法1和方法2的區(qū)別:
兩種方法各有優(yōu)劣, 用Comparable 簡單, 只要實現(xiàn)Comparable 接口的對象直接就成為一個可以比較的對象,但是需要修改源代碼。
用Comparator 的好處是不需要修改源代碼, 而是另外實現(xiàn)一個比較器, 當(dāng)某個自定義的對象需要作比較的時候,把比較器和對象一起傳遞過去就可以比大小了, 并且在Comparator 里面用戶可以自己實現(xiàn)復(fù)雜的可以通用的邏輯,使其可以匹配一些比較簡單的對象,那樣就可以節(jié)省很多重復(fù)勞動了。
(2)使用HashSet來實現(xiàn)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
package com.package1; import java.util.*; public class StuScore { public static void main(String[] args) { HashSet<Student> hs= new HashSet<Student>(); //添加元素進去 for ( int i= 20070301 ,j= 1 ;i<= 20070330 ;i++,j++) { hs.add( new Student(i, "同學(xué)" +j,( int ) ( 40 *Math.random()+ 60 ))); } ArrayList<Student>li= new ArrayList(hs); Collections.sort(li, new Sortbygrade()); for (Student ss:li) System.out.println(ss); } } //學(xué)生類 class Student { int num; int grade; String name; public Student( int num, String name, int grade) { this .num=num; this .name=name; this .grade=grade; } public String toString(){ //System.out.println("hi"); return "學(xué)號:" + this .num+ "\t" + "姓名:" + this .name + " " + "成績:" + this .grade; } } class Sortbygrade implements Comparator{ @Override public int compare(Object o1, Object o2) { Student s1=(Student) o1; Student s2=(Student) o2; if (s1.grade>s2.grade) return 1 ; if (s1.grade<s2.grade) return - 1 ; // if(s1.grade==s2.grade) return 0 ; } } |
輸出結(jié)果如下:
學(xué)號:20070310 姓名:同學(xué)19 成績:60
學(xué)號:20070330 姓名:同學(xué)39 成績:62
學(xué)號:20070326 姓名:同學(xué)35 成績:63
學(xué)號:20070317 姓名:同學(xué)26 成績:64
學(xué)號:20070318 姓名:同學(xué)27 成績:65
學(xué)號:20070322 姓名:同學(xué)31 成績:65
學(xué)號:20070301 姓名:同學(xué)10 成績:67
學(xué)號:20070328 姓名:同學(xué)37 成績:68
學(xué)號:20070304 姓名:同學(xué)13 成績:68
學(xué)號:20070319 姓名:同學(xué)28 成績:69
學(xué)號:20070313 姓名:同學(xué)22 成績:70
學(xué)號:20070303 姓名:同學(xué)12 成績:71
學(xué)號:20070312 姓名:同學(xué)21 成績:71
學(xué)號:20070329 姓名:同學(xué)38 成績:72
學(xué)號:20070306 姓名:同學(xué)15 成績:72
學(xué)號:20070324 姓名:同學(xué)33 成績:72
學(xué)號:20070305 姓名:同學(xué)14 成績:75
學(xué)號:20070315 姓名:同學(xué)24 成績:75
學(xué)號:20070314 姓名:同學(xué)23 成績:78
學(xué)號:20070307 姓名:同學(xué)16 成績:80
學(xué)號:20070311 姓名:同學(xué)20 成績:81
學(xué)號:20070302 姓名:同學(xué)11 成績:83
學(xué)號:20070309 姓名:同學(xué)18 成績:84
學(xué)號:20070320 姓名:同學(xué)29 成績:85
學(xué)號:20070321 姓名:同學(xué)30 成績:85
學(xué)號:20070316 姓名:同學(xué)25 成績:86
學(xué)號:20070327 姓名:同學(xué)36 成績:90
學(xué)號:20070308 姓名:同學(xué)17 成績:94
學(xué)號:20070323 姓名:同學(xué)32 成績:94
學(xué)號:20070325 姓名:同學(xué)34 成績:95
對HashSet的總結(jié):
1、HashSet中的元素不可以重復(fù),如果重復(fù)添加,則只會顯示一個。
原理如下:
HashSet:底層數(shù)據(jù)結(jié)構(gòu)是哈希表。是線程不安全的。不同步。
2、HashSet是如何保證元素唯一性的呢?
答:是通過元素的兩個方法,hashCode和equals來完成。
如果元素的HashCode值相同,才會判斷equals是否為true。如果元素的hashcode值不同,不會調(diào)用equals。
3、對HashSet的排序,通過將Set集合轉(zhuǎn)化為List集合,借助Collections.Sort( )方法實現(xiàn)排序。
3、使用TreeMap來實現(xiàn)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
package com.package1; import java.util.Comparator; import java.util.Iterator; import java.util.Set; import java.util.TreeMap; public class TestTreeMap { public static void main(String[] args) { //1.創(chuàng)建集合 TreeMap<Student,Integer> tm= new TreeMap<Student,Integer>(); for ( int i= 20070301 ,j= 10 ;i<= 20070330 ;i++,j++) { int grade=( int ) ( 40 *Math.random()+ 60 ); //2、往集合對象中添加元素 tm.put( new Student(grade, "同學(xué)" +j),i); } //3.遍歷集合 ,排序完成 Set<Student> k=tm.keySet(); Iterator<Student> it=k.iterator(); while (it.hasNext()){ Student key=it.next(); Integer num=tm.get(key); System.out.println( "學(xué)號:" +num+ " " + "姓名:" +key.name+ " " + "成績:" +key.grade); } } } class Student implements Comparable<Student>{ int grade; String name; public Student( int grade,String name){ this .grade =grade; this .name=name; } @Override public int compareTo(Student o) { if ( this .grade>o.grade) return 1 ; if ( this .grade==o.grade) { //當(dāng)成績相同時,按照姓名排序 return this .name.compareTo(o.name); } return - 1 ; } } |
輸出結(jié)果為:
學(xué)號:20070303 姓名:同學(xué)12 成績:61
學(xué)號:20070323 姓名:同學(xué)32 成績:61
學(xué)號:20070317 姓名:同學(xué)26 成績:62
學(xué)號:20070309 姓名:同學(xué)18 成績:64
學(xué)號:20070301 姓名:同學(xué)10 成績:67
學(xué)號:20070304 姓名:同學(xué)13 成績:69
學(xué)號:20070322 姓名:同學(xué)31 成績:69
學(xué)號:20070328 姓名:同學(xué)37 成績:70
學(xué)號:20070305 姓名:同學(xué)14 成績:71
學(xué)號:20070319 姓名:同學(xué)28 成績:73
學(xué)號:20070321 姓名:同學(xué)30 成績:74
學(xué)號:20070310 姓名:同學(xué)19 成績:81
學(xué)號:20070315 姓名:同學(xué)24 成績:82
學(xué)號:20070307 姓名:同學(xué)16 成績:84
學(xué)號:20070330 姓名:同學(xué)39 成績:84
學(xué)號:20070312 姓名:同學(xué)21 成績:85
學(xué)號:20070324 姓名:同學(xué)33 成績:87
學(xué)號:20070306 姓名:同學(xué)15 成績:88
學(xué)號:20070308 姓名:同學(xué)17 成績:90
學(xué)號:20070327 姓名:同學(xué)36 成績:90
學(xué)號:20070318 姓名:同學(xué)27 成績:91
學(xué)號:20070316 姓名:同學(xué)25 成績:92
學(xué)號:20070320 姓名:同學(xué)29 成績:92
學(xué)號:20070314 姓名:同學(xué)23 成績:93
學(xué)號:20070313 姓名:同學(xué)22 成績:94
學(xué)號:20070302 姓名:同學(xué)11 成績:95
學(xué)號:20070325 姓名:同學(xué)34 成績:95
學(xué)號:20070329 姓名:同學(xué)38 成績:97
學(xué)號:20070326 姓名:同學(xué)35 成績:98
學(xué)號:20070311 姓名:同學(xué)20 成績:99
對TreeMap的總結(jié):
1、TreeMap默認對key進行排序,所以可將自定義對象放入key中,將代表學(xué)號的整型放入value中。對Key排序時,可以指定自定義對象中的某個屬性來排序。
2、Map集合使用put()方法添加元素。
3、Map集合的取出原理:將map集合轉(zhuǎn)成set集合。在通過迭代器取出。map集合的兩種取出方式:
(1)Set<k> keySet:將map中所有的鍵存入到Set集合。因為set具備迭代器。所有可以迭代方式取出所有的鍵,在根據(jù)get方法。獲取每一個鍵對應(yīng)的值。
(2)Set<Map.Entry<k,v>> entrySet:將map集合中的映射關(guān)系存入到了set集合中,而這個關(guān)系的數(shù)據(jù)類型就是:Map.Entry
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:http://blog.csdn.net/qq_25827845/article/details/51909531