package day21;
import java.util.ArrayList;
import java.util.Collections;/*** @author monian* Wo yi wu ta,wei shou shu er!*/
public class Homework01 {@SuppressWarnings({"all"})public static void main(String[] args) {News news1 = new News("新冠确诊病例超千万,数百万印度教信徒赴恒河“圣浴”引民众担忧");News news2 = new News("男子突然想起2个月前钓的鱼还在网兜里,捞起一看赶紧放生");ArrayList arrayList = new ArrayList();arrayList.add(news1);arrayList.add(news2);Collections.reverse(arrayList);System.out.println(arrayList);System.out.println("打印新闻:");for (Object o :arrayList) {News newone = (News)o;System.out.println(newone.getTitle());}}
}
class News{private String title;public News(String title) {this.title = title;}private String context;@Overridepublic String toString() {return "News "+"title='" + title;}public String getTitle() {char[] chartit= new char[15];if(title.length()>15){for (int i = 0; i < 15; i++) {chartit[i] = title.charAt(i);}}
// String tit = new String(chartit);String tit = String.valueOf(chartit);return tit+"..";}public void setTitle(String title) {this.title = title;}public String getContext() {return context;}public void setContext(String context) {this.context = context;}
}
打印结果:
public static void main(String[] args) {ArrayList arrayList = new ArrayList();arrayList.add(new News("新冠确诊病例超千万,数百万印度教信徒赴恒河\"圣浴\"引民众担忧"));arrayList.add(new News("男子突然想起2个月前钓的鱼还在网兜里,捞起一看赶紧放生"));int size = arrayList.size();for (int i = size-1; i >=0 ; i--) {
// System.out.println(arrayList.get(i));News news = (News) arrayList.get(i);System.out.println(proccessTitle(news.getTitle()));}}//专门写一个方法处理,新闻标题proccess处理public static String proccessTitle(String title){if(title.length()==0){return "";}if (title.length()>15){return title.substring(0,15)+"..";}else{return title;}}
}class News{private String title;public News(String title) {this.title = title;}private String context;@Overridepublic String toString() {return "News "+"title='" + title;}public String getTitle() {return title;}public void setTitle(String title) {this.title = title;}public String getContext() {return context;}public void setContext(String context) {this.context = context;}
}