1、作业一:
package hsf.com.HomeWork_;
import java.util.ArrayList;
public class HomeWork01_News {
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) + "...";
} else {
return title;
}
}
}
class News {
private String title;
private String contains;
public News(String title) {
this.title = title;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContains() {
return contains;
}
public void setContains(String contains) {
this.contains = contains;
}
@Override
public String toString() {
return title;
}
}
作业二:
package hsf.com.HomeWork_;
import java.util.ArrayList;
import java.util.Iterator;
public class HomeWork02 {
public static void main(String[] args) {
ArrayList arrayList = new ArrayList();
arrayList.add(new Car("马自达", 100000));
arrayList.add(new Car("宝马", 200000));
arrayList.add(new Car("宾利", 1000000));
arrayList.remove(0);
System.out.println(arrayList.size());
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 brand;
private double price;
public Car(String brand, double price) {
this.brand = brand;
this.price = price;
}
@Override
public String toString() {
return brand + " " + price;
}
}
作业三:
package hsf.com.HomeWork_;
import java.util.*;
public class HomeWork03 {
public static void main(String[] args) {
Map map = new HashMap();
map.put("jack", 650);
map.put("tom", 1200);
map.put("smith", 2900);
map.put("jack", 2690);
Set keySet = map.keySet();
for (Object key : keySet) {
map.put(key, (Integer)map.get(key) + 100);
System.out.println(map.get(key));
}
Set entrySet = map.entrySet();
Iterator iterator = entrySet.iterator();
while (iterator.hasNext()) {
Map.Entry entry = (Map.Entry) iterator.next();
System.out.println(entry.getKey() + " " + entry.getValue());
}
Collection values = map.values();
for (Object value : values) {
System.out.println(value);
}
}
}
class Employee {
private String name;
private double sal;
public Employee(String name, double sal) {
this.name = name;
this.sal = sal;
}
@Override
public String toString() {
return name + " " + sal;
}
}
作业四:
作业五:分析代码
TreeSet treeSet = new TreeSet();
treeSet.add(new Employee());
作业六:
package hsf.com.set_;
import java.util.HashSet;
import java.util.Objects;
public class HomeWork04 {
public static void main(String[] args) {
HashSet set = new HashSet();
Person_ p1 = new Person_(1001,"AA");
Person_ p2 = new Person_(1002,"BB");
set.add(p1);
set.add(p2);
p1.name = "CC";
set.remove(p1);
System.out.println(set);
set.add(new Person_(1001,"CC"));
System.out.println(set);
set.add(new Person_(1001,"AA"));
System.out.println(set);
}
}
class Person_ {
public int ID;
public String name;
public Person_(int ID, String name) {
this.ID = ID;
this.name = name;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Person_ person_ = (Person_) o;
return ID == person_.ID && Objects.equals(name, person_.name);
}
@Override
public int hashCode() {
return Objects.hash(ID, name);
}
}
作业七: