diff --git a/day07/src/com/inmind/arraylist03/Book.java b/day07/src/com/inmind/arraylist03/Book.java new file mode 100644 index 0000000..65c057f --- /dev/null +++ b/day07/src/com/inmind/arraylist03/Book.java @@ -0,0 +1,74 @@ +package com.inmind.arraylist03; +/* +* 最贵的书。 + +* 定义一个图书Book类。 + + - 属性:图书编号,书名,价格,出版日期 + - 构造方法: + - 无参构造方法,满参构造方法 + - 成员方法: + - get/set方法 + - showBook方法,输出图书信息 + +* 定义测试类,使用满参构造方法,创建三(多)个Book对象,判断价格最贵的图书(存放书的数组的遍历),并输出图书信息。 + + */ +public class Book { + private String bid; + private String bName; + private double price; + private String date; + + public Book() { + } + + public Book(String bid, String bName, double price, String date) { + this.bid = bid; + this.bName = bName; + this.price = price; + this.date = date; + } + + public String getBid() { + return bid; + } + + public void setBid(String bid) { + this.bid = bid; + } + + public String getbName() { + return bName; + } + + public void setbName(String bName) { + this.bName = bName; + } + + public double getPrice() { + return price; + } + + public void setPrice(double price) { + this.price = price; + } + + public String getDate() { + return date; + } + + public void setDate(String date) { + this.date = date; + } + + + public void showBook() { + System.out.println("图书信息{" + + "图书编号='" + bid + '\'' + + ", 书名='" + bName + '\'' + + ", 价格=" + price + + ", 日期='" + date + '\'' + + '}'); + } +} diff --git a/day07/src/com/inmind/arraylist03/Demo05.java b/day07/src/com/inmind/arraylist03/Demo05.java new file mode 100644 index 0000000..d8fe147 --- /dev/null +++ b/day07/src/com/inmind/arraylist03/Demo05.java @@ -0,0 +1,16 @@ +package com.inmind.arraylist03; + +public class Demo05 { + public static void main(String[] args) { + //创建一个保存书的容器 + Book[] arr = new Book[3]; + arr[0] = new Book(); + arr[1] = new Book(); + arr[2] = new Book(); + + //Book[]:对象数组,还想再加2本书??不能,数组是长度固定,arr中无法再添加新的书,除非重新new一个新数组 + //对象数组能直接删除一个书对象??不能,只能修改 + //为了解决以上的问题,有个更好的容器,ArrayList集合 + //ArrayList的特点:1.长度可变,2.可以增删改查 3.可以存放任意的引用类型,(基本数据类型不能保存到集合中!!) + } +} diff --git a/day07/src/com/inmind/arraylist03/Demo06.java b/day07/src/com/inmind/arraylist03/Demo06.java new file mode 100644 index 0000000..830cfa4 --- /dev/null +++ b/day07/src/com/inmind/arraylist03/Demo06.java @@ -0,0 +1,26 @@ +package com.inmind.arraylist03; + +import java.util.ArrayList; + +/* +注意:ArrayList在使用时,将同一种数据类型保存在一个容器,直接指定泛型 + */ +public class Demo06 { + public static void main(String[] args) { + //创建一个集合容器 + ArrayList list = new ArrayList<>();//如果泛型不写,默认保存Object类型的数据,任意的引用类型的祖宗 + list.add(1); + list.add(1.1); + list.add(true); + list.add("hehe"); + list.add(new Book()); + + //创建一个存放字符串的集合容器 + ArrayList list1 = new ArrayList<>(); + list1.add("张三"); + list1.add("李四"); + System.out.println(list1);//list1保存的是地址,但是由于底层源码实现,修改了输出结果,原本是地址,转为该地址对应的内容了 + + System.out.println("程序结束"); + } +}