JAVA集合练习

这篇博客介绍了Java集合的实战应用,包括创建Worker对象的List,进行增删改查操作,避免字符串重复,生成不重复的随机数列表并排序,实现Book对象的排序和输出,以及统计特定姓氏学生的平均成绩和筛选数组中符合条件的数字到List。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.已知有一个Worker 类如下:

public class Worker {

private int age;

private String name;

private double salary;

public Worker (){

}

public Worker (String name, int age, double salary){

this.name = name;

this.age = age;

this.salary = salary;

}

public int getAge() { return age; }

public void setAge(int age) { this.age = age; }

public String getName() { return name; }

public void setName(String name) { this.name = name; }

public double getSalary(){ return salary; }

public void setSalary(double salary){ this.salary = salary; }

public void work(){

System.out.println(name + “ work”);

}

}

完成下面的要求

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 方法。

        List<Worker> workers = new ArrayList<>();
        workers.add(new Worker("zhang3", 18, 3000));
        workers.add(new Worker("li4", 25, 3500));
        workers.add(new Worker("wang5", 22, 3200));
        System.out.println(workers);
        int index = 0;
        for (int i = 0; i < workers.size(); i++) {
            if (workers.get(i).getName().equals("li4")) {
                index = i;
            }
        }
        System.out.println("li4在集合中的下标:" + index);
        //插入zhao6
        workers.add(index, new Worker("zhao6", 24, 3300));
        System.out.println(workers);
        //删除wang5
        Iterator<Worker> it = workers.iterator();
        while (it.hasNext()){//是否有下一个元素
            Worker worker = it.next();//获取下一个元素
            if (worker.getName().equals("wang5")){
                it.remove();//必须使用迭代器的删除方法
//                workers.remove(worker);//运行时报错
            }
        }
        //for循环遍历
        System.out.println(workers);
        for (Worker worker:workers ) {
            System.out.println(worker);
        }
        //利用迭代器遍历
        Iterator<Worker> it2 = workers.iterator();
        while (it2.hasNext()){
            Worker worker = it2.next();
            worker.work();
        }

2.去除集合中字符串的重复值(要求使用 ArrayList)

执行结果如下:

旧集合为:[李玉伟, 李嘉诚, 马化腾, 刘强东, 李玉伟, 王健林, 马云, 雷军]

新集合为:[李玉伟, 李嘉诚, 马化腾, 刘强东, 王健林, 马云, 雷军]

        String[] strArr = {"李玉伟", "李嘉诚", "马化腾", "刘强东","李玉伟" , "王健林", "马云", "雷军"};
        //数组转集合
        List<String> list1 = Arrays.asList(strArr);
        //asList转化的底层代码不一样,重新接收一下,否则报错
        List<String> list = new ArrayList<>(list1);
//        Object[] object = list.toArray();//集合转 数组
        for (int i = 0; i < list.size(); i++) {
            for (int j = i+1; j < list.size(); j++) {
                if (list.get(i).equals(list.get(j))){
                    list.remove(j);
                    j--;//删除后往前移一位
                }
            }
        }
        System.out.println(list1);
        System.out.println(list);

3.分析以下需求,并用代码实现:(使用ArrayList)

(1)生成10个1至100之间的随机整数(不能重复),存入一个List集合

(2)编写方法对List集合进行排序

(2)然后利用迭代器遍历集合元素并输出

(3)如:15 18 20 40 46 60 65 70 75 91

List<Integer> list = new ArrayList<>();
        while (list.size() < 10){
            int ranNum = new Random().nextInt(1,101);
            if (!list.contains(ranNum)){
                list.add(ranNum);
            }
        }
        System.out.println(list);
        System.out.println(list.size());
 
        List<Integer> sortList = sortList(list);
        System.out.println(sortList);
 
        Iterator<Integer> it = sortList.iterator();
        while (it.hasNext()){
            Integer i = it.next();
            System.out.print(i + " ");
        }
//排序方法-------------
    public static List<Integer> sortList(List<Integer> list){
        for (int i = 0; i < list.size()-1; i++) {
            for (int j = 0; j < list.size() - i - 1; j++) {
                if (list.get(j) > list.get(j+1)){
                    int temp = list.get(j);
                    list.set(j,list.get(j+1));
                    list.set(j+1,temp);
                }
            }
        }
        return list;
    }

4.编写一个类Book,具有name,price,press(出版社),author 然后创建5个对象放入ArrayList中,并实现按照price大小排序,

然后遍历ArrayList输出每个Book对象, 使用toString 方法打印。

        Book book1 = new Book("《从尸解仙开始》", 100.5, "起点中文网", "鳄鱼皮的皮");
        Book book2 = new Book("《光阴之外》", 200.5, "起点中文网", "耳根");
        Book book3 = new Book("《深空彼岸》", 180.5, "起点中文网", "辰东");
        Book book4 = new Book("《明克街13号》", 130.5, "起点中文网", "纯洁滴小龙");
        Book book5 = new Book("《灵境行者》", 210.5, "起点中文网", "卖报小郎君");
        List<Book> books = new ArrayList<>();
        books.add(book1);
        books.add(book2);
        books.add(book3);
        books.add(book4);
        books.add(book5);
        System.out.println(books);
 
        BookComparator bookComparator = new BookComparator();
        books.sort(bookComparator);
        System.out.println(books);
 
        System.out.println("------------");
        for (Book book : books) {
            System.out.println(book);
        }
 
//book类------------
public class Book {
    private String name;
    private double price;
    private String press;
    private String author;
 
    public Book() {
    }
 
    public Book(String name, double price, String press, String author) {
        this.name = name;
        this.price = price;
        this.press = press;
        this.author = author;
    }
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public double getPrice() {
        return price;
    }
 
    public void setPrice(double price) {
        this.price = price;
    }
 
    public String getPress() {
        return press;
    }
 
    public void setPress(String press) {
        this.press = press;
    }
 
    public String getAuthor() {
        return author;
    }
 
    public void setAuthor(String author) {
        this.author = author;
    }
 
    @Override
    public String toString() {
        return "Book{" +
                "name='" + name + '\'' +
                ", price=" + price +
                ", press='" + press + '\'' +
                ", author='" + author + '\'' +
                '}';
    }
}
 
//book比较方法------------
import java.util.Comparator;
 
// 定义 书类型的  比较器
public class BookComparator implements Comparator<Book> {
 
    @Override
    public int compare(Book b1, Book b2) {
        // 定义比较规则
        if (b1.getPrice() > b2.getPrice()){
            return 1;
        }else if (b1.getPrice() < b2.getPrice()){
            return -1;
        }
        return 0;
    }
}

5.使用List集合存储10个学生信息。

学生信息:姓名,年龄,成绩。

统计所有姓“张”的同学的平均成绩。

        List<Student> studentList = new ArrayList<>();
        studentList.add(new Student("张三", 20 , 90) );
        studentList.add(new Student("赵三", 23 , 67) );
        studentList.add(new Student("李三", 20 , 54) );
        studentList.add(new Student("张四", 20 , 87) );
        studentList.add(new Student("张二", 20 , 57) );
        studentList.add(new Student("张六", 20 , 87) );
        studentList.add(new Student("张七", 20 , 34) );
        studentList.add(new Student("王十一", 20 , 67) );
        studentList.add(new Student("王三", 20 , 98) );
        studentList.add(new Student("张三", 20 , 80) );
 
        double sumScore = 0; // 姓张的同学的总成绩
        int index = 0 ;// 姓张的同学的人数
        for (int i = 0; i < studentList.size(); i++) {
            if (studentList.get(i).getName().startsWith("张")){
                index ++; // 姓张的同学的人数 + 1
                sumScore += studentList.get(i).getScore();
            }
        }
        double avg = sumScore / index ;
        System.out.println("平均分:" + avg);
  1. 产生10个1-100的随机数,并放到一个数组中,把数组中大于等于10的数字放到一个list集合中,并打印到控制台

        int[] arr = new int[10];
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < arr.length; i++) {
            arr[i] = (int)(Math.random() * 100 + 1);
            if (arr[i] > 10){
                list.add(arr[i]);
            }
        }
        System.out.println(Arrays.toString(arr));
        System.out.println(list);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值