
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栈中的字符串,所以我们需要对处理后的字符串接收

被折叠的 条评论
为什么被折叠?



