From 45eef578c7f0af46b323e750ed5b4af8887574b2 Mon Sep 17 00:00:00 2001 From: xuxin <840198532@qq.com> Date: Tue, 22 Sep 2026 10:27:37 +0800 Subject: [PATCH] =?UTF-8?q?day07--=E5=B8=B8=E7=94=A8=E7=B1=BB-ArrayList-?= =?UTF-8?q?=E7=BB=83=E4=B9=A02=5F=E6=B7=BB=E5=8A=A0=E5=AF=B9=E8=B1=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- day07/src/com/inmind/arraylist03/Demo09.java | 29 ++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 day07/src/com/inmind/arraylist03/Demo09.java diff --git a/day07/src/com/inmind/arraylist03/Demo09.java b/day07/src/com/inmind/arraylist03/Demo09.java new file mode 100644 index 0000000..a57b34e --- /dev/null +++ b/day07/src/com/inmind/arraylist03/Demo09.java @@ -0,0 +1,29 @@ +package com.inmind.arraylist03; + +import java.util.ArrayList; + +/* +常用类-ArrayList-练习2_添加对象 + */ +public class Demo09 { + //创建一个保存book对象的容器,添加3本书,通过集合遍历判断出哪本书最贵 + public static void main(String[] args) { + ArrayList bookList = new ArrayList<>(); + //先创建3本书,或者在添加时,进行创建 + //Book book1 = new Book("b001", "书1", 10.0, "2020-1-1"); + Book book2 = new Book("b001", "书2", 10.0, "2020-1-1"); + Book book3 = new Book("b001", "书3", 50.0, "2020-1-1"); + bookList.add(new Book("b001", "书1", 30.0, "2020-1-1")); + bookList.add(book2); + bookList.add(book3); + //集合的变量 + Book maxBook = bookList.get(0); + for (int i = 0; i < bookList.size(); i++) { + Book book = bookList.get(i); + if (book.getPrice() > maxBook.getPrice()) { + maxBook = book; + } + } + maxBook.showBook(); + } +}