Java-ArrayList应用实例-信息管理

示例代码如下:

  • 需求:

操作类
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Scanner;

public class NoticeOperator {
    public static void main(String[] args) {
        //初始化并赋值公告;
        NoticeInformation noticeInformation1 = new NoticeInformation(01,"欢迎访问我的CSDN博客","作者",new Date());
        NoticeInformation noticeInformation2 = new NoticeInformation(02,"文章有错误请斧正,大家共同进步","爸爸",new Date());
        NoticeInformation noticeInformation3 = new NoticeInformation(03,"欢迎下次光临","老鸨",new Date());

        NoticeOperator noticeOperator = new NoticeOperator();
        //把公告信息放入List中
        List arrayList = new ArrayList();
        arrayList.add(noticeInformation1);
        arrayList.add(noticeInformation2);
        arrayList.add(noticeInformation3);
        ///1.取出公告的具体标题和创建者
        System.out.println("公告的具体内容和创建者是:");
        for(int i=0; i<arrayList.size(); i++){
            //从集合中取出一部分noticeInformation,例如利用getTitle(),getCreator()取出公告的标题和公告的创建者
            //因为任何数据在放入集合中就变成了Object类型,
            // 所以我们需要使用强制类型转换,让他记得自己是NoticeInformation类型的!
            String noticeTitle =((NoticeInformation)(arrayList.get(i))).getTitle();
            String noticeCreator =((NoticeInformation)(arrayList.get(i))).getCreator();
            System.out.println(i+1+":"+noticeTitle+"---"+noticeCreator);
        }

        //2.修改公告信息
        System.out.println("因为一些不为人知的原因,我修改一下公告的创建者");
          //利用一下NoticeInformation 的setCreator()方法.
        ((NoticeInformation)(arrayList.get(2))).setCreator("博主");
                ///同样我们可以用第二种方法修改某个值;
        /*  noticeInformation3.setCreator("博主");
            arrayList.set(2,noticeInformation3);
         */
            ///输出修改后的creator
        for(int i=0; i<arrayList.size(); i++){
            String creator = ((NoticeInformation)(arrayList.get(i))).getCreator();
            System.out.println("修改后公告的创建者为: "+creator);
        }
    }
}

///信息类
import java.util.Date;
public class NoticeInformation {
    private int id;
    private String title;
    private String creator;
    private Date creatTime;

    public NoticeInformation(int id, String title, String creator, Date creatTime) {
        this.id = id;
        this.title = title;
        this.creator = creator;
        this.creatTime = creatTime;
    }

    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 getCreator() {
        return creator;
    }

    public void setCreator(String creator) {
        this.creator = creator;
    }

    public Date getCreatTime() {
        return creatTime;
    }

    public void setCreatTime(Date creatTime) {
        this.creatTime = creatTime;
    }
}
  • 运行结果:
posted @   青松城  阅读(29)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示