日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

2020.8.8List、Set集合练习

發布時間:2023/12/20 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 2020.8.8List、Set集合练习 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

01_LinkedList集合練習

案例演示
需求:編寫一個程序,
獲取10個1至20的隨機數,
要求隨機數不能重復。

package LinkedListDemo01;import java.util.LinkedHashSet; import java.util.Random; import java.util.Set; import java.util.TreeSet;public class LinkedListTest01 {public static void main(String[] args) {/* A:案例演示需求:編寫一個程序,獲取10個1至20的隨機數,要求隨機數不能重復。Random();1,不能重復,說明元素唯一,且有序,所以使用LinkedHashSet2.長度小于103,是整數4,輸出*/Random random = new Random();TreeSet<Integer> treeSet = new TreeSet<>();while (treeSet.size()<10){int num = random.nextInt(20);treeSet.add(num);}System.out.println(treeSet);//[1, 5, 6, 7, 10, 11, 13, 16, 17, 18]} }

02_TreeSet集合練習

案例演示:學生類

案例演示:
需求:鍵盤錄入3個學生信息(姓名, 語文成績, 數學成績, 英語成績),
按照總分從高到低輸出到控制臺。
1.創建student類
2.鍵盤錄入4個信息
3.把3個學生對象輸入到集合中
4.遍歷出來

package LinkedListDemo01;public class Student {private String name;private int chineseScore;private int mathScore;private int englishScore;public Student() {}public Student(String name, int chineseScore, int mathScore, int englishScore) {this.name = name;this.chineseScore = chineseScore;this.mathScore = mathScore;this.englishScore = englishScore;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getChineseScore() {return chineseScore;}public void setChineseScore(int chineseScore) {this.chineseScore = chineseScore;}public int getMathScore() {return mathScore;}public void setMathScore(int mathScore) {this.mathScore = mathScore;}public int getEnglishScore() {return englishScore;}public void setEnglishScore(int englishScore) {this.englishScore = englishScore;}//獲取總分的方法public int getTotalScore(){return chineseScore+englishScore+mathScore;}}

測試類

package LinkedListDemo01;import java.util.Comparator; import java.util.Scanner; import java.util.TreeMap; import java.util.TreeSet;public class TreeSetTest01 {/*A:案例演示:需求:鍵盤錄入3個學生信息(姓名, 語文成績, 數學成績, 英語成績),按照總分從高到低輸出到控制臺。1.創建student類2.鍵盤錄入4個信息3.把3個學生對象輸入到集合中4.遍歷出來*/public static void main(String[] args) {Student student = new Student();//匿名內部類打印TreeSet<Student> treeSet = new TreeSet<>(new Comparator<Student>() {@Overridepublic int compare(Student a, Student b) {//先按成績排名,成績相等按名字排名int num=a.getTotalScore()-b.getTotalScore();int num1=num==0? a.getName().compareTo(b.getName()):num;return num1;}});for (int i = 0; i < 4; i++) {Scanner sc1 = new Scanner(System.in);System.out.println("請輸入第"+i+"個學生的姓名");String name = sc1.nextLine();student.setName(name);System.out.println("請輸入第" + i + "個學生的語文成績");int yw = sc1.nextInt();student.setChineseScore(yw);System.out.println("請輸入第" + i + "個學生的數學成績");int sx= sc1.nextInt();student.setMathScore(sx);System.out.println("請輸入第" + i + "個學生的英語成績");int yy = sc1.nextInt();student.setEnglishScore(yy);}System.out.println("名次\t姓名\t語文\t數學\t英語\t總分");int i=0;for (Student students : treeSet) {System.out.println((++i)+"\t"+student.getName()+"\t"+student.getChineseScore()+"\t"+student.getMathScore()+"\t"+student.getEnglishScore()+"\t"+student.getTotalScore());}}}

總結

以上是生活随笔為你收集整理的2020.8.8List、Set集合练习的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。