新闻类

  • 新闻类
    public class News {

    /** 新闻编号 /
    private int id;
    /
    * 新闻标题 /
    private String title;
    /
    * 新闻作者 */
    private String author;
    public int getId() {
    return id;
    }
    public void setId(int id) {
    this.id = id;
    }
    public String getTitle() {
    return title;
    }
    public void setTitle(String title) {
    this.title = title;
    }
    public String getAuthor() {
    return author;
    }
    public void setAuthor(String author) {
    this.author = author;
    }
    public News() {
    super();
    }
    public News(int id, String title, String author) {
    super();
    this.id = id;
    this.title = title;
    this.author = author;
    }
    @Override
    public String toString() {
    return “News [id=” + id + “, title=” + title + “, author=” + author + “]”;
    }

测试类
public static void main(String[] args) {

	// 创建集合对象 List(有序  不唯一) -> ArrayList(可变数组)
	// <> 泛型 规范需要添加
	// News[] news = new News[];
	List<News> list = new ArrayList<News>();
	
	// 存储数据
	// news[0] = xxx;  news[1] = xxx;
	News news1 = new News(1, "震惊!巴黎圣母院失火!", "李天");
	News news2 = new News(2, "震惊!LOL玩家和DOTA玩家竟然在众人面前互斥对方算什么男人!", "浩然");
	News news3 = new News(3, "是中国人必须转!不转不是中国人!", "国强");
	
	// boolean add = list.add(news1);
	// 存储数据
	list.add(news1); 
	list.add(news2);
	list.add(news3);
	
	// 获取总数
	// news.length()      length  len size lenOf...
	int size = list.size();
	System.out.println("新闻的总数:"+size);
	
	News news4 = new News(4, "震惊!李义半夜起床走向窗外....", "陈");
	// 向指定的索引位置添加数据 原有数据依次后移
	list.add(0, news4);
	// 遍历集合
	// 遍历数组  因为数组有索引 所以采用遍历索引 遍历数组
	/*
	 * for(int i = 0; i < news.length; i++){
	 * 		news[i]
	 * }
	 */
	for(int i = 0; i < list.size(); i++) {
		// 集合根据索引获取数据
		News news = list.get(i);
		System.out.println(news.getTitle()+" ");
	}
	
	System.out.println("是否包含李天相关新闻:"+list.contains(news4));

	// 删除李天数据
	// list.remove(news4);
	// 删除指定索引的数据 并且返回被删除的数据
	list.remove(0);
	System.out.println("是否包含李相关新闻:"+list.contains(news4));
	Collections.shuffle(list);
}

}

猜你喜欢

转载自blog.csdn.net/Javastudenthhhk/article/details/89360677
今日推荐