KC20230104 - JAVA List集合 + Set集合

问题1

/**
 * 完成下面的要求
 * 1) 创建一个List,在List 中增加三个工人,基本信息如下:
 * 姓名      年龄    工资
 * zhang3   18      3000
 * li4           25      3500
 * wang5    22      3200
 * 2) 在li4 之前插入一个工人,信息为:姓名:zhao6,年龄:24,工资3300
 * 3) 删除wang5 的信息
 * 4) 利用for 循环遍历,打印List 中所有工人的信息
 * 5) 利用迭代遍历,对List 中所有的工人调用work 方法。
 */

public class q1 {
    public static void main(String[] args) {
        List<Worker> li = new ArrayList<>();
        //1) 创建一个List,在List 中增加三个工人,基本信息如下:
        Worker wr1 = new Worker("zhang3", 18, 3000);
        Worker wr2 = new Worker("li4", 25, 3500);
        Worker wr3 = new Worker("wang5", 22, 3200);
        li.add(wr1);
        li.add(wr2);
        li.add(wr3);

        //  2) 在li4 之前插入一个工人,信息为:姓名:zhao6,年龄:24,工资3300
        Worker wr4 = new Worker("zhao6", 24, 3300);
        int pos = 0;
        for (int i = 0; i < li.size(); i++) {
            if (li.get(i).getName().equals("li4")) {
                pos = i;
                break;
            }
        }
        li.add(pos, wr4);

        // 3) 删除wang5 的信息
        //迭代器遍历
        Iterator<Worker> it = li.iterator();
        while (it.hasNext()) {
            if (it.next().getName().equals("wang5")) {
                it.remove();
            }
        }

        // 4) 利用for 循环遍历,打印List 中所有工人的信息
        for (int i = 0; i < li.size(); i++) {
            System.out.println(li.get(i));
        }
        //5) 利用迭代遍历,对List 中所有的工人调用work 方法。

        Iterator<Worker> it2 = li.iterator();
        while (it2.hasNext()) {
            it2.next().work();
        }

    }
}

问题2

/*
* 2.去除集合中字符串的重复值(要求使用 ArrayList)
  执行结果如下:
   旧集合为:[李玉伟, 李嘉诚, 马化腾, 刘强东, 李玉伟, 王健林, 马云, 雷军]
   新集合为:[李玉伟, 李嘉诚, 马化腾, 刘强东, 王健林, 马云, 雷军]
   * "李玉伟", "李嘉诚","马化腾", "刘强东", "李玉伟", "王健林", "马云", "雷军"
*
* */
public class q2 {
    public static void main(String[] args) {
        ArrayList<String> ar = new ArrayList();
        //一次性添加
        Set<String> s = new LinkedHashSet(Arrays.asList("李玉伟", "李嘉诚", "马化腾", "刘强东", "李玉伟", "王健林", "马云", "雷军"));

        ar.addAll(s);

    }

}

问题3

/*
* (1)生成10个1至100之间的随机整数(不能重复),存入一个List集合
(2)编写方法对List集合进行排序
(2)然后利用迭代器遍历集合元素并输出
(3)如:15 18 20 40 46 60 65 70 75 91
* */
public class q3 {
    public static void main(String[] args) {
        //(1)生成10个1至100之间的随机整数(不能重复),存入一个List集合
        List<Integer> list = new ArrayList();
        for (int i = 0; i < 10; ) {
            Random random = new Random();
            // 随机1~100个数
            int r = (int )(Math.random() * 100 + 1);

            if (!list.contains(r)) {
                list.add(r);
                i++;
            }

        }
        // (2)编写方法对List集合进行排序
        // 冒泡算法
        mao(list);
        // 2 调用sort函数
        // Collections.sort(list);
        // 3 然后利用迭代器遍历集合元素并输出
        for (Iterator iter = list.iterator(); iter.hasNext(); ) {
            int temp = (int) iter.next();
            System.out.println(temp);
        }

    }
    public  static void mao(List<Integer> list){
        for (int i = 1; i < list.size(); i++) {
            for (int j = 1; j <= list.size() - i; j++) {
                if (list.get(j - 1) > list.get(j)) {
                    int t = list.get(j - 1);
                    list.set(j - 1, list.get(j));
                    list.set(j, t);
                }
            }
        }

    }
}

问题4

public class q4 {
    public static void main(String[] args) {

        //编写一个类Book,具有name,price,press(出版社),author 然后创建5个对象放入ArrayList中
        ArrayList<Book> list = new ArrayList<Book>();
        Book b1 = new Book("书1", 50, "北极出版社", "作者1");
        Book b2 = new Book("书2", 70, "南极出版社", "作者2");
        Book b3 = new Book("书3", 60, "天堂出版社", "作者3");
        Book b4 = new Book("书4", 80, "地狱出版社", "作者4");
        Book b5 = new Book("书5", 10, "人间出版社", "作者5");
        list.add(b1);
        list.add(b2);
        list.add(b3);
        list.add(b4);
        list.add(b5);
        //根据price从小到大排序
        list.sort(new Comparator<Book>() {
            @Override
            public int compare(Book o1, Book o2) {

                //降序
                if(o1.price>o2.price)
                    return -1;
                else
                    return 1;
            }
        });

        //遍历ArrayList输出每个Book对象, 使用toString 方法打印。
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i).toString());
        }
    }
}

class Book {
    String name;
    double price;
    String press;
    String author;

    public Book(String name, int price, String press, String author) {
        this.name = name;
        this.price = price;
        this.press = press;
        this.author = author;
    }

    @Override
    public String toString() {
        return "Book{" +
                "name='" + name + '\'' +
                ", price=" + price +
                ", press='" + press + '\'' +
                ", author='" + author + '\'' +
                '}';
    }

}

问题5

/*
* 使用List集合存储10个学生信息。
学生信息:姓名,年龄,成绩。
统计所有姓“张”的同学的平均成绩。
*
* */
public class q5 {
    public static void main(String[] args) {
        /*
         * .使用List集合存储10个学生信息。学生信息:姓名,年龄,成绩。
         *
         * */
        Student s1 = new Student("张三", 21, 90);
        Student s2 = new Student("李四", 22, 77);
        Student s3 = new Student("王五", 18, 44);
        Student s4 = new Student("张二", 15, 60);
        Student s5 = new Student("王六", 17, 11);
        Student s6 = new Student("李一", 19, 20);
        Student s7 = new Student("白九", 20, 40);
        Student s8 = new Student("张八", 20, 66);
        Student s9 = new Student("赵二", 18, 80);
        Student s10 = new Student("孙十", 18, 88);


        List<Student> list = new ArrayList<>();
        list.add(s1);
        list.add(s2);
        list.add(s3);
        list.add(s4);
        list.add(s5);
        list.add(s6);
        list.add(s7);
        list.add(s8);
        list.add(s9);
        list.add(s10);
        //统计所有姓“张”的同学的平均成绩。
        double sum = 0;
        int num = 0;
        for (int i = 0; i < 10; i++) {
            //截取第一个字
            String s = list.get(i).name.substring(0, 1);

            if (s.equals("张")) {
                num++;
                sum += list.get(i).score;

            }
        }
        System.out.println(sum / num);
    }
}

class Student {
    String name;
    int age;
    int score;

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

问题6

/*
 * 产生10个1-100的随机数,并放到一个数组中,把数组中大于等于10的数字放到一个list集合中,并打印到控制台

 * */
public class q6 {
    public static void main(String[] args) {
        int[] num = new int[10];

        List<Integer> list = new ArrayList();
        //产生10个1-100的随机数,并放到一个数组中
        for (int i = 0; i < 10; i++) {
            num[i] = (int) (Math.random() * 99 + 1);

        }
        // 把数组中大于等于10的数字放到一个list集合中,并打印到控制台
        for (int i = 0; i < 10; i++) {
            if (num[i] >= 10) {
                list.add(num[i]);
                System.out.println(num[i]);
            }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值