集合 练习1

按要求实现:
 (1)封装一个新闻类,包含标题和内容属性,提供get、set方法,重写toString方法,打印对象时只打印标题;
 (2)只提供一个带参数的构造器,实例化对象时,只初始化标题;并且实例化两个对象:
 新闻一:新冠确诊病例超千万,数百万印度教信徒赴恒河“圣浴”引民众担忧
 新闻二:男子突然想起2个月前钓的鱼还在网兜里,捞起一看赶紧放生
 (3)将新闻对象添加到ArrayList集合中,并且进行倒序遍历;
 (4)在遍历集合过程中,对新闻标题进行处理,超过15字的只保留前15个,然后在后边加“…"
 (5)在控制台打印遍历出经过处理的新闻标题;
public class Homework01 {
    public static void main(String[] args) {
        ArrayList arrayList = new ArrayList<>();
        arrayList.add(new News("新冠确诊病例超千万,数百万印度教信徒赴恒河\"圣浴\"引民众担忧"));
        arrayList.add(new News("男子突然想起2个月前钓的鱼还在网兜里,捞起一看赶紧放生"));

        Collections.reverse(arrayList); //将集合倒序
        for (int i = 0; i < arrayList.size(); 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)
        }
        else return title;
    }
}
class News{
    private String title;
    private String content;

    public News(String title) {
        this.title = title;
    }

    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;
    }

    @Override
    public String toString() {
        return "News{" +
                "title='" + title + '\'' +
                '}';
    }
} 
   

输出:

男子突然想起2个月前钓的鱼还在...
新冠确诊病例超千万,数百万印度...

使用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方法

public class Homework02 {
    public static void main(String[] args) {
        ArrayList arrayList = new ArrayList<>();
        Car car = new Car("宝马",400000);
        Car car1 = new Car("宾利",3000000);

        arrayList.add(car); //添加
        arrayList.add(car1); //添加
        System.out.println(arrayList); //[Car{name='宝马', price=400000.0}, Car{name='宾利', price=3000000.0}]
        arrayList.remove(car); //删除
        System.out.println(arrayList); //[Car{name='宾利', price=3000000.0}]
        System.out.println(arrayList.contains(car)); //false
        System.out.println(arrayList.size()); //1
        System.out.println(arrayList.isEmpty()); //false
        arrayList.addAll(arrayList);
        System.out.println(arrayList); //[Car{name='宾利', price=3000000.0}, Car{name='宾利', price=3000000.0}]
        System.out.println(arrayList.containsAll(arrayList)); //true

        System.out.println("-----------增强for-------------");
        for (Object o :arrayList) {
            System.out.println(o);
        }
        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 "Car{" +
                "name='" + name + '\'' +
                ", price=" + price +
                '}';
    }
} 
   

输出:

[Car{name='宝马', price=400000.0}, Car{name='宾利', price=3000000.0}]
[Car{name='宾利', price=3000000.0}]
false
1
false
[Car{name='宾利', price=3000000.0}, Car{name='宾利', price=3000000.0}]
true
-----------增强for-------------
Car{name='宾利', price=3000000.0}
Car{name='宾利', price=3000000.0}
-----------迭代器-------------
Car{name='宾利', price=3000000.0}
Car{name='宾利', price=3000000.0}

你可能感兴趣的:(java,servlet,数据结构)