30天学会JAVA—练习题(2021韩顺平)——Day15
集合练习题
public class A01 {
@SuppressWarnings("all")
public static void main(String[] args) {
ArrayList arrayList = new ArrayList();
arrayList.add(new Newspaper("新冠确诊病例超千万,数百万印度教信徒赴恒河“圣浴”引民众担忧"));
arrayList.add(new Newspaper("男子突然想起2个月前钓的鱼还在网兜里,捞起一看赶紧放生"));
//倒序遍历
for(int i = arrayList.size()-1; i >= 0; i--){
//向下转型
Newspaper newspaper = (Newspaper) arrayList.get(i);
System.out.println( processTitle(newspaper.getTitle()));
}
}
//处理新闻标题
public static String processTitle(String title){
if(title == null){
return "";
}
if(title.length() > 15){
return title.substring(0, 15) + "...";
}else{
return title;
}
}
}
class Newspaper{
private String title;
private String content;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public Newspaper(String title) {
super();
this.title = title;
}
@Override
public String toString() {
return "\n"+"Newspaper [title=" + title + "]";
}
}
public class A02 {
@SuppressWarnings("all")
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(0);
System.out.println("删除宝马后:"+arrayList);
System.out.println("查找宾利:"+arrayList.contains("宾利"));
System.out.println("获取元素个数:"+arrayList.size());
System.out.println("判断是否为空:"+arrayList.isEmpty());
//判断是否为空
//清空
// arrayList.clear();
// System.out.println(arrayList);
System.out.println("添加多个元素"+arrayList.addAll(arrayList));
//查找多个元素是否存在
//增强for循环遍历
for(Object obj:arrayList){
System.out.println(obj);
}
//迭代器遍历
Iterator iterator = arrayList.iterator();
while(iterator.hasNext()){
System.out.println(iterator.next());
}
}
}
class Car{
private String name;
private int price;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public Car(String name, int price) {
super();
this.name = name;
this.price = price;
}
@Override
public String toString() {
return "\n"+"Car [name=" + name + ", price=" + price + "]";
}
}
public class A03 {
@SuppressWarnings("all")
public static void main(String[] args) {
Map hashMap = new HashMap();
hashMap.put("jack", 650);
hashMap.put("tom", 1200);
hashMap.put("smith", 2900);
System.out.println(hashMap);
hashMap.replace("jack", 650, 2600);
System.out.println(hashMap);
//遍历【 keySet】:为所有员工加薪100元
Set keySet = hashMap.keySet();
for(Object obj : keySet){
hashMap.put(obj, (Integer)hashMap.get(obj)+100);
}
System.out.println(hashMap);
//entrySet + 迭代器
Set entrySet = hashMap.entrySet();
Iterator iterator = entrySet.iterator();
while(iterator.hasNext()){
Map.Entry next = (Entry) iterator.next();
System.out.println(next.getKey() + "-" + next.getValue());
}
Collection values = hashMap.values();
for(Object value : values){
System.out.println(value);
}
}
}
额……