Basic data structure exercises

1. Define a method listTest(ArrayList list, String name) to return the index of the first occurrence of name in the list, and return -1 if name does not appear before.

2. The known array stores a batch of QQ numbers, the length is 5-11 digits, String[] strs = {"10001","10086","12347806666","45612378901","10001","12347806666"}. Store all the qq numbers in the array in the LinkedList, delete the duplicate elements in the list, and print out all the elements in the list with an iterator and an enhanced for loop.

3. Please randomly generate a double-color ball number. (Require the same color number not to be repeated). Two-color ball rules: Each bet on the two-color ball consists of 6 red ball numbers and 1 blue ball number. The red ball number is selected from 1 to 33; the blue ball number is selected from 1 to 16;

4. Use the Comparable and Comparator interfaces to sort the scores of the following four students in descending order. If the scores are the same, they will be sorted in descending order based on the scores.
Insert picture description here

code show as below:

1.

public class ListTestDemo {
    
    
    public static void main(String[] args) {
    
    
        ArrayList<String> nameDirectory = new ArrayList<>();
        nameDirectory.add("张三");
        nameDirectory.add("李四");
        nameDirectory.add("王五");
        nameDirectory.add("刘六");

        while(true){
    
    
            System.out.println("=======名字检索系统=======");
            Scanner scanner = new Scanner(System.in);
            String input = scanner.nextLine();
            int index = listTest(nameDirectory,input);
            if(index == -1){
    
    
                System.out.println(input + "不在该链表中");
            }else{
    
    
                System.out.println(input + "的index为" + index);
            }
        }
    }
    public static int listTest(ArrayList<String> list, String name){
    
    
        int len = list.size();
        for(int i = 0; i < len; i++){
    
    
            if(list.get(i).equals(name)){
    
    
                return i;
            }
        }
        return -1;
    }
}

2.

public class RemoveRepeat {
    
    
    public static void main(String[] args) {
    
    
        String[] strs = {
    
    "10001", "10086", "12347806666", "45612378901", "10001", "12347806666"};
        LinkedList<String> list = getList(strs);
        //增强型for循环
        System.out.println("------增强型for循环------");
        for(String i : list){
    
    
            System.out.println(i);
        }
        //迭代器
        System.out.println("---------迭代器---------");
        Iterator<String> iterator = list.iterator();
        while(iterator.hasNext()){
    
    
            String s = iterator.next();
            System.out.println(s);
        }
    }

    public static LinkedList<String> getList(String[] strs){
    
    
        LinkedList<String> list = new LinkedList<String>();
        for(int i=0; i<strs.length; i++){
    
    
            if(list.contains(strs[i])){
    
    
                continue;
            }
            list.add(strs[i]);
        }
        return list;
    }
}

3

public class BallNumber {
    
    
    public static void main(String[] args) {
    
    
        while(true){
    
    
            menu();
        }

    }

    public static int menu(){
    
    
        System.out.println("======欢迎来到双色球游戏======");
        System.out.println("请输入相应编号继续游戏:");
        System.out.println("1-开始游戏  2-退出游戏");
        Scanner scanner = new Scanner(System.in);
        String input = scanner.nextLine();
        int num = -1;
        try{
    
    
            num = Integer.parseInt(input);
        }catch(NumberFormatException e){
    
    
            System.out.println("请输入数字");
            return menu();
        }
        if(num < 1 || num > 2){
    
    
            System.out.println("请输入正确的数字");
            return menu();
        }

        if(num == 1){
    
    
            getball();
        }else{
    
    
            System.exit(1);
        }
        return -1;
    }
    public static void getball(){
    
    
        System.out.println("正在生成一注双色球号码...");
        System.out.println("生成成功!");
        System.out.println("红色球号码为:");
        LinkedList<Integer> redBall = getRedBall();
        for(Integer i : redBall){
    
    
            System.out.print(i);
            System.out.print('\t');
        }
        System.out.println();//换行
        System.out.println("蓝色球号码为:");
        int blueNum = getBlueBall();
        System.out.println(blueNum);
    }
    public static LinkedList<Integer> getRedBall(){
    
    
        LinkedList<Integer> red = new LinkedList<>();
        Random random = new Random();
        while(red.size() < 6){
    
    
            int code = (int)(1 + Math.random() * 33);
            if(red.contains(code)){
    
    
                continue;
            }
            red.add(code);
        }
        return red;
    }

    public static int getBlueBall(){
    
    
        Random random = new Random();
        return (int)(1 + Math.random() * 16);
    }

}

4

public class compareTest {
    
    

    public static void main(String[] args) {
    
    
        Student s1 = new Student("贾宝玉",14,88.5);
        Student s2 = new Student("林黛玉",13,90.5);
        Student s3 = new Student("史湘云",13,85.5);
        Student s4 = new Student("薛宝钗",15,91.0);
        Student s5 = new Student("工具人",15,88.5);
        ArrayList<Student> studentList = new ArrayList<>();
        studentList.add(s1);
        studentList.add(s2);
        studentList.add(s3);
        studentList.add(s4);
        studentList.add(s5);
        System.out.println("--------comparable实现方法--------");
        System.out.println("原来的顺序");
        for(Student s : studentList){
    
    
            System.out.println(s);
        }
        System.out.println("--------------"+'\n' + "现在的顺序:");
        Collections.sort(studentList);
        for(Student s : studentList){
    
    
            System.out.println(s);
        }

        System.out.println("--------comparator实现方法--------");
        System.out.println("原来的顺序");
        for(Student s : studentList){
    
    
            System.out.println(s);
        }
        System.out.println("--------------"+'\n' + "现在的顺序:");
        Collections.sort(studentList,new comparator());
        for(Student s : studentList){
    
    
            System.out.println(s);
        }
    }

    static class Student implements Comparable<Student>{
    
    
        private String name;
        private int age;
        private double points;

        public Student(String name, int age, double points) {
    
    
            this.name = name;
            this.age = age;
            this.points = points;
        }

        @Override
        public String toString() {
    
    
            return "Student{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    ", points=" + points +
                    '}';
        }

        @Override
        public int compareTo(Student o) {
    
    
            if(o.points == this.points){
    
    
                return this.age - o.age;
            }
            if(o.points - this.points > 0){
    
    
                return 1;
            }else if(o.points - this.points < 0){
    
    
                return -1;
            }else{
    
    
              return 0;
            }

        }
    }

    static class comparator implements Comparator<Student> {
    
    

        @Override
        public int compare(Student o1, Student o2) {
    
    
            if(o1.points == o2.points){
    
    
                return o1.age - o2.age;
            }

            if(o1.points - o2.points > 0){
    
    
                return -1;
            }else if(o1.points - o2.points < 0){
    
    
                return 1;
            }else{
    
    
                return 0;
            }
        }
    }
}

Guess you like

Origin blog.csdn.net/m0_55221239/article/details/114444657