题解_数据结构基础练习

题目:

1、定义一个方法 listTest(ArrayList list, String name),要求返回name 在 list 里面第一次出现的索引,如果 name 没出现过返回-1。

2、已知数组存放一批 QQ 号码,长度 5-11 位,
String[] strs = {“10001”,“10086”,“12347806666”,“45612378901”,“10001”,“12347806666”}。
将该数组里面的所有 qq 号都存放在 LinkedList 中,将 list 中重复元素删除,将 list 中所有元素分别用迭代器和增强 for 循环打印出来。

3、分别用Comparable和Comparator两个接口对下列四位同学的成绩做降
序排序,如果成绩一样,那在成绩排序的基础上按照年龄由小到大排序。
在这里插入图片描述

源代码:

第1题:

public class ArrayListTest {
     
    public static void main(String[] args) {
     
        Scanner in = new Scanner(System.in);
        ArrayList<String> nameList = new ArrayList<>();
        nameList.add("李白");
        nameList.add("杜甫");
        nameList.add("白居易");

        while (true){
     
            System.out.println("--------请输入要查询的姓名--------");
            String name = in.nextLine();
            int index = listTest(nameList, name);
            if (index == -1){
     
                System.out.println("----------查无此人---------\n");
            } else{
     
                System.out.println(name + "的index是:" + index + "\n");
            }
        }
    }

    public static int listTest(ArrayList<String> list, String name){
     
        int size = list.size();
        for (int i = 0; i < size; i++) {
     
            if (list.get(i).equals(name)){
     
                return i;
            }
        }
        return -1;
    }
}

第2题:

public class LinkedListTest {
     
    public static void main(String[] args) {
     
        String[] strs = {
     "10001","10086","12347806666","45612378901","10001","12347806666"};
        LinkedList<String> qqList = getList(strs);

        // 迭代器打印
        System.out.println("------------迭代器------------");
        Iterator<String> i = qqList.iterator();
        while (i.hasNext()){
     
            System.out.print(i.next() + " ");
        }

        System.out.println();

        // 增强for循环打印
        System.out.println("----------增强for循环---------");
        for (String name: qqList) {
     
            System.out.print(name +" ");
        }


    }

    // 获取无重复的List
    public static LinkedList<String> getList(String[] strs){
     
        LinkedList<String> list = new LinkedList<>();
        for (int i = 0; i < strs.length; i++) {
     
            if (!list.contains(strs[i])){
     
                list.add(strs[i]);
            }
        }
        return list;
    }
}

第3题:

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);
        Student s4 = new Student("薛宝钗", 15, 91);
        Student s5 = new Student("王熙凤", 16, 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("原顺序:");
        for (Student s : studentList) {
     
            System.out.println(s);
        }

        System.out.println("------------Comparable实现-----------");
        System.out.println("排序后顺序:");
        Collections.sort(studentList);
        for (Student s : studentList) {
     
            System.out.println(s);
        }

        System.out.println("------------Comparator实现-----------");
        System.out.println("排序后顺序:");
        Collections.sort(studentList, new StudentComparator());
        for (Student s : studentList) {
     
            System.out.println(s);
        }

    }

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

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

         @Override
         public String toString() {
     
             return "姓名:" + name +
                     ", 年龄:" + age +
                     ", 分数:" + score ;
         }

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

    static class StudentComparator implements Comparator<Student>{
     

        @Override
        public int compare(Student o1, Student o2) {
     
            if (o1.score == o2.score){
     
                return o1.age - o2.age;
            }
            if (o1.score - o2.score > 0){
     
                return -1;
            }else {
     
                return 1;
            }
        }
    }
}

你可能感兴趣的:(题解_数据结构基础练习)