第一题
public static void main(String[] args) {
ArrayList arrayList = new ArrayList<>();
arrayList.add(new News(" 新冠确诊病例超千万,数百万印度教信徒赴恒河\"圣浴\"引民众担忧"));
arrayList.add(new News("男子突然想起2个月前钓的鱼还在网兜里,捞起一看赶紧放生"));
//倒序遍历
for (int i = arrayList.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)+"..."; //取前面15个
}else {
return title;
}
}
class News{
private String content;
private String title;
public News(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
@Override
public String toString() {
return "title='" + title + '\'';
}
}
运行结果:
第二题
public static void main(String[] args) {
ArrayList arrayList = new ArrayList<>();
//添加元素
arrayList.add(new Car("宝马",400000));
arrayList.add(new Car("宾利",5000000));
System.out.println(arrayList);
//删除指定的元素
arrayList.remove(1);
System.out.println(arrayList);
//查看元素是否存在
System.out.println(arrayList.contains(1));
//获取元素个数
System.out.println("元素个数为:"+arrayList.size());
//判断集合是否为空
System.out.println(arrayList.isEmpty());
//清空
// arrayList.clear();
//添加多个元素
arrayList.addAll(arrayList);
System.out.println(arrayList);
//查找多个元素是否存在
arrayList.containsAll(arrayList);
//删除多个元素
// arrayList.removeAll(arrayList);
//遍历
System.out.println("==========增强for==========");
for (Object A:arrayList){
System.out.println(A);
}
System.out.println("==========迭代器==========");
Iterator iterator = arrayList.iterator();
while (iterator.hasNext()){
Object next=iterator.next();
System.out.println(next);
}
}
class Car{
private String name;
private double price;
public Car(String name, double price) {
this.name = name;
this.price = price;
}
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;
}
@Override
public String toString() {
return "name='" + name + '\'' +
", price=" + price;
}
}
运行结果:
第三题
public static void main(String[] args) {
HashMap Map=new HashMap<>();
Map.put("jack",650);
Map.put("tom",1200);
Map.put("smith",2900);
System.out.println(Map);
Map.replace("jack",2600);
System.out.println(Map);
//所有人加100
Set KeySet=Map.keySet();
for (Object key:KeySet){
Map.put(key,(Integer)Map.get(key)+100);
}
System.out.println(Map);
//遍历集合中所有的员工
Set set = Map.entrySet();
Iterator iterator = set.iterator();
while (iterator.hasNext()){
Map.Entry next=(Map.Entry)iterator.next();
System.out.println(next.getKey()+"-----"+next.getValue());
}
//遍历集合中所有的工资
Collection values = Map.values();
System.out.println(values);
}
class Employee{
private String name;
private double salary;
public Employee(String name, double salary) {
this.name = name;
this.salary = salary;
}
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;
}
@Override
public String toString() {
return name + '\'' +
"-----" + salary+"元";
}
}
运行结果:
第四题
第五题
解决方法:Person 去实现 Comparable 接口