1 CollectionTest01.java
按照要求实现:
(1)封装一个新闻类,包含标题和内容属性,提供get、set方法,重写toString方法,并打印对象时只打印标题;
(2)只提供一个带参数的构造器,实例化对象时,只初始化标题,并且实例化两个对象:
新闻一:新冠确诊病例超千万,数百万印度教徒赴恒河“圣浴”
引民众担忧;
新闻二:男子突然想起2个月前钓的鱼还在网兜里,捞起一看赶紧放生;
(3)将新闻对象添加到ArrayList集合中,并进行倒序遍历;
(4)在遍历集合的过程中,将新闻标题进行处理,超过15字的只保留前15个字,然后加上"…";
(5)在控制台打印结果。
🐱 参考代码及运行结果:
import java.util.ArrayList; /** * @author 兴趣使然黄小黄 * @version 1.0 */ public class CollectionTest01 { public static void main(String[] args) { ArrayList arrayList = new ArrayList(); arrayList.add(new News("新冠确诊病例超千万,数百万印度教徒赴恒河\"圣浴\"引民众担忧")); arrayList.add(new News("男子突然想起2个月前钓的鱼还在网兜里,捞起一看赶紧放生")); // 倒叙遍历 int size = arrayList.size(); for (int i = size - 1; i >= 0; i--) { News news = (News) arrayList.get(i); System.out.println(processTitle(news.getTitle())); } } // 处理新闻标题 public static String processTitle(String title) { if (title == null) { return ""; } if (title.length() > 15) { return title.substring(0, 15); // [0, 15) } return title; } } class News { private String title; private String content; public News(String title) { this.title = title; } public String getTitle() { return title; } public String getContent() { return content; } public void setTitle(String title) { this.title = title; } public void setContent(String content) { this.content = content; } @Override public String toString() { return "News{" + "title='" + title + '\'' + '}'; } }
2 CollectionTest02.java
使用ArrayList完成对 对象 Car{name,price}的各种操作
(1)add:添加单个元素;
(2)remove:删除指定元素;
(3)contains:查找元素是否存在;
(4)size:获取元素的个数;
(5)isEmpty:判断是否为空;
(6)clear:清空;
(7)addAll:添加多个元素;
(8)containsAll:查找多个元素是否都存在;
(9)removeAll:删除多个元素;
使用增强for和迭代器来遍历所有的car,需要重写Car的toString方法。
🐰 参考代码及运行结果:
import java.util.ArrayList; import java.util.Iterator; /** * @author 兴趣使然黄小黄 * @version 1.0 */ public class CollectionTest02 { public static void main(String[] args) { ArrayList arrayList = new ArrayList(); Car car1 = new Car("宝马", 400000); Car car2 = new Car("宾利", 5000000); // 添加 arrayList.add(car1); arrayList.add(car2); System.out.println("add:" + arrayList); // 删除 arrayList.remove(car1); System.out.println("remove:" + arrayList); // 判断 System.out.println(arrayList.contains(car2)); System.out.println(arrayList.isEmpty()); // 元素个数 System.out.println(arrayList.size()); // 清空 arrayList.clear(); System.out.println(arrayList); // 添加多个 arrayList.add(car1); arrayList.add(car2); arrayList.addAll(arrayList); System.out.println(arrayList); // 查找多个是否存在 System.out.println(arrayList.containsAll(arrayList)); for (Object o : arrayList) { System.out.println(o); } Iterator iterator = arrayList.iterator(); while (iterator.hasNext()) { Object next = iterator.next(); System.out.println(next); } } } class Car { private String name; private int price; public Car(String name, int price) { this.name = name; this.price = price; } @Override public String toString() { return "Car{" + "name='" + name + '\'' + ", price=" + price + '}'; } public String getName() { return name; } public void setName(String name) { this.name = name; } public void setPrice(int price) { this.price = price; } public int getPrice() { return price; } }