String类和List集合综合起来的一个题

import java.util.ArrayList;

public class test{
    public static void main(String[] args) {

        ArrayList arrayList = new ArrayList();

        News news1 = new News("新闻1:在新冠下,印度的和水污染严重---------");
        News news2 = new News("新闻2:一个男子钓鱼失败了==============");

        arrayList.add(news1);
        arrayList.add(news2);

        for (int i = arrayList.size() - 1; i >= 0 ; i--) {
            News news  = (News)(arrayList.get(i));
            news.setTitle(process(news.getTitle()));
            System.out.println(arrayList.get(i));
        }

    }
    public static String process(String str){
        if(str == null || str.length() ==0){
            throw new RuntimeException("标题有误");
        }

        if(str.length() > 15){
            StringBuffer stringbuffer = new StringBuffer( str.substring(0,15));
            stringbuffer.append("***");
            return stringbuffer.toString();
        }
        return str;
    }

}
class News{
    private String title;
    private String maintext;

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

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getMaintext() {
        return maintext;
    }

    public void setMaintext(String maintext) {
        this.maintext = maintext;
    }

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

这个题的主要的一个核心点就是,我们要注意到String类型的不可变性,你在方法中对字符串进行一系列修改时,实际上不会影响到main栈中的字符串,所以我们需要对处理后的字符串接收

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值