1. 程式人生 > >集合實例練習

集合實例練習

集合實例 集合求隨機數 集合錄入自定義類排序 自然排序選擇器排序 集合嵌套

1、鍵盤錄入5個學生信息(姓名,語文成績,數學成績,英語成績),按照總分從高到低輸出到控制臺。
a:自然排序
測試類:
`package treeSet集合;

import java.util.Comparator;
import java.util.Scanner;
import java.util.TreeSet;

public class ZuoyeScannerSelect {
public static void main(String[] args) {
TreeSet<StudentSelect> ts=new TreeSet<StudentSelect>(new Comparator<StudentSelect>() {

        @Override
        public int compare(StudentSelect s1, StudentSelect s2) {
            int num1=s2.sum(s2)-s1.sum(s1);
            int num2=(num1==0)?s2.getName().compareTo(s1.getName()):num1;
            return num2;
        }

    }) ;
    System.out.println("請輸入學生信息:");
    //錄入學生信息
    for(int x=1;x<6;x++) {
        Scanner sc=new Scanner(System.in);
        System.out.print("請輸入第"+x+"個學生的姓名:");
        //錄入學生姓名
        String name=sc.nextLine();
        System.out.print("請輸入第"+x+"個學生的語文成績:");
        //錄入學生語文成績
        int chineseScore=sc.nextInt();
        System.out.print("請輸入第"+x+"個學生的數學成績:");
        //錄入學生數學成績
        int mathScore=sc.nextInt();
        System.out.print("請輸入第"+x+"個學生的英語成績:");
        //錄入學生英語成績
        int englishScore=sc.nextInt();
        //將學生信息封裝到學生對象裏
        StudentSelect s=new StudentSelect();
        s.setName(name);
        s.setChineseScore(chineseScore);
        s.setMathScore(mathScore);
        s.setEnglishScore(englishScore);
        //把學生對象添加到集合中
        ts.add(s);
    }
    //遍歷
    for(StudentSelect s:ts) {
        System.out.println(s.getName()+","+ + s.getChineseScore()+","
    + s.getMathScore()+","+ s.getEnglishScore()+","+s.sum(s));
    }
}   

}`
實體類:

package treeSet集合;

public class StudentZiran implements Comparable<StudentZiran>{
    //自然排序,自定義類中實現Comparable接口
    private String name;
    private int chineseScore;
    private int mathScore;
    private int englishScore;

    public StudentZiran() {//無參構造
        // TODO Auto-generated constructor stub
    }
    //有參構造
    public StudentZiran(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 sum(StudentZiran s) {
        return s.getChineseScore()+s.getMathScore()+s.getEnglishScore();
    }
    //重寫compearTo()方法
    @Override
    public int compareTo(StudentZiran s) {
        //主要排序條件:總成績,按總成績從高到低
        int num1=s.sum(s)-this.sum(this);
        //次要排序條件,當總成績相同時按學生姓名內容比較
        int num2=(num1==0)?s.getName().compareTo(this.getName()):num1;
        return num2;
    }   
}
    b:選擇器排序
    測試類:
    `package treeSet集合;

import java.util.Comparator;
import java.util.Scanner;
import java.util.TreeSet;

public class ZuoyeScannerSelect {
public static void main(String[] args) {
TreeSet<StudentSelect> ts=new TreeSet<StudentSelect>(new Comparator<StudentSelect>() {

        @Override
        public int compare(StudentSelect s1, StudentSelect s2) {
            int num1=s2.sum(s2)-s1.sum(s1);
            int num2=(num1==0)?s2.getName().compareTo(s1.getName()):num1;
            return num2;
        }

    }) ;
    System.out.println("請輸入學生信息:");
    //錄入學生信息
    for(int x=1;x<6;x++) {
        Scanner sc=new Scanner(System.in);
        System.out.print("請輸入第"+x+"個學生的姓名:");
        //錄入學生姓名
        String name=sc.nextLine();
        System.out.print("請輸入第"+x+"個學生的語文成績:");
        //錄入學生語文成績
        int chineseScore=sc.nextInt();
        System.out.print("請輸入第"+x+"個學生的數學成績:");
        //錄入學生數學成績
        int mathScore=sc.nextInt();
        System.out.print("請輸入第"+x+"個學生的英語成績:");
        //錄入學生英語成績
        int englishScore=sc.nextInt();
        //將學生信息封裝到學生對象裏
        StudentSelect s=new StudentSelect();
        s.setName(name);
        s.setChineseScore(chineseScore);
        s.setMathScore(mathScore);
        s.setEnglishScore(englishScore);
        //把學生對象添加到集合中
        ts.add(s);
    }

    //遍歷
    for(StudentSelect s:ts) {
        System.out.println(s.getName()+","+ + s.getChineseScore()+","
    + s.getMathScore()+","+ s.getEnglishScore()+","+s.sum(s));
    }
}   

}`
實體類:

package treeSet集合;

public class StudentSelect {
    private String name;
    private int chineseScore;
    private int mathScore;
    private int englishScore;

    public StudentSelect() {//無參構造
        // TODO Auto-generated constructor stub
    }
    //有參構造
    public StudentSelect(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 static int sum(StudentSelect s) {
        return s.getChineseScore()+s.getMathScore()+s.getEnglishScore();
    }

}

2、獲取10隨機數,1-20之間,要求值不能重復!(ArrayList,HashSet)
ArrayList:

package 集合獲取隨機數;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Random;

public class ArrayListGetRandom {
    public static void main(String[] args) {
        //創建集合al
        ArrayList<Integer> al=new ArrayList<Integer>();
        //創建新集合newal
        ArrayList<Integer> newal=new ArrayList<Integer>();
        //判斷新集合newal中的元素個數是否夠10個,若不夠,繼續添加
        while(newal.size()<10) {
            //創建獲取隨機數對象
            Random r=new Random();
            //獲取隨機數
            int n=r.nextInt(20);
            //將獲取到的隨機數添加到集合al集合
            al.add(n);
            //遍歷舊集合
            Iterator<Integer> it=al.iterator();
            while(it.hasNext()) {
               int i=it.next();
               //判斷newal中是否包含al中的元素,若不包含,將al中的元素添加到newal中
               if(!newal.contains(i)) {
                   newal.add(i);
               }
            }
            //遍歷newal集合
            for(int i:newal) {
               System.out.println(i);
            }
        }
    }
}

HashSet:

package 集合獲取隨機數;

import java.util.HashSet;
import java.util.Random;
import java.util.Set;

public class HashSetGetRandom {
    public static void main(String[] args) {
        //創建Set集合對象
        Set<Integer> set=new HashSet<Integer>();
        //創建獲取隨機數類對象
        Random r=new Random();
        //判斷已獲取隨機數個數,若不夠10個,則繼續添加
        while(set.size()<10) {
            //獲取隨機數
            int n=r.nextInt(20);
              //將獲取的隨機數添加到集合set中
              set.add(n);      
        }
        //增強for循環遍歷集合set
        for(int i:set) {
             System.out.println(i);
        }
    }
}

3、ArrayList集合嵌套HashMap集合並遍歷。
需求:
假設ArrayList集合的元素是HashMap。有3個。
每一個HashMap集合的鍵和值都是字符串。
元素如下,請遍歷。

ArrayList<HashMap<String,String>>

結果:
周瑜---小喬
呂布---貂蟬

郭靖---黃蓉
楊過---小龍女

令狐沖---任盈盈
林平之---嶽靈珊
`package 集合嵌套;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;

public class AM {
public static void main(String[] args) {
//創建一個大的ArrayList集合am
ArrayList<HashMap<String,String>> am=new ArrayList<HashMap<String,String>>();
//創建第一個HashMap集合hm1
HashMap<String,String> hm1=new HashMap<String,String>();
//給集合hm1添加元素
hm1.put("周瑜","小喬");
hm1.put("呂布","貂蟬");
//將hm1的元素添加到大集合am中
am.add(hm1);
//創建第二個HashMap集合hm2
HashMap<String,String> hm2=new HashMap<String,String>();
//給集合hm2添加元素
hm2.put("郭靖","黃蓉");
hm2.put("楊過","小龍女");
//將hm2的元素添加到大集合am中
am.add(hm2);
//創建第三個HashMap集合hm3
HashMap<String,String> hm3=new HashMap<String,String>();
//給集合hm3添加元素
hm3.put("令狐沖","任盈盈");
hm3.put("林平之","嶽靈珊");
//將hm3的元素添加到大集合am中
am.add(hm3);
//遍歷大集合am
for(HashMap<String,String> ahm:am) {
Set<String> set=ahm.keySet();
for(String key:set) {
String value=ahm.get(key);
System.out.println(key+"---"+value);
}
System.out.println();
}
}
}`

集合實例練習