List集合常用方法的使用

ListDemo1.java

add()方法添加元素
get(int index) 获取下标对应的元素
遍历list需要使用fot(;;)循环
contains()是否包含
remove()删除元素,可以是删除下标所对应,也可以是删除对象

import java.util.ArrayList;
import java.util.List;

public class ListDemo1 {

    public static void main(String[] args) {
        // 用ArrayList存储编程语言名称,并输出

        // 创建list的时候会在内存中开辟一块内存空间 内存空间是连续的
        List list = new ArrayList();
        list.add("Java");
        list.add("C");
        list.add("C++");
        list.add("Go");
        list.add("Swift");

        // 输出列表中元素个数
        System.out.println("列表中元素个数为:" + list.size());

        // 遍历 list中提供get方法获取下标
        for (int i = 0; i < list.size(); i++)
            System.out.print(list.get(i)+", ");

        // 移除C++ index方式
        list.remove(2);
        // remove对象的方式
        list.remove("C++");
        System.out.println();
        System.out.println("移除C++后的列表元素为:");
        for (int i = 0; i < list.size(); i++)
            System.out.print(list.get(i)+", ");
    }
}

在list中存储对象

假设一个公告类对象Notice.java

import java.util.Date;

public class Notice {

    private int id;
    private String title;
    private String creator;

    private Date createTime;

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

    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 getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }
}

建立测试类进行测试,特别注意打印内容时需要调用类对象的某一属性方法,需要将父类对象进行强制转换
包装的noticeList.get(i)是一个Object对象,(Notice) (noticeList.get(i))进行强制转换

import java.util.ArrayList;
import java.util.Date;

public class NoticeTest {
    public static void main(String[] args) {
        // 公告对象
        Notice notice1 = new Notice(1, "公告1", "people1", new Date());
        Notice notice2 = new Notice(2, "公告2", "people2", new Date());
        Notice notice3 = new Notice(3, "公告3", "people3", new Date());

        // list存储公告
        ArrayList noticeList = new ArrayList();
        noticeList.add(notice1);
        noticeList.add(notice2);
        noticeList.add(notice3);

        // 显示公告
        for (int i = 0; i < noticeList.size(); i++)
            // 非常注意,父类对象强制转换成子类对象
            System.out.println(i + 1 + ":" + ((Notice) (noticeList.get(i))).getTitle());

        System.out.println("——————下面插入添加新公告");

        // 添加
        Notice notice4 = new Notice(4, "公告4", "people4", new Date());
        noticeList.add(2, notice4);
        // 显示公告
        for (int i = 0; i < noticeList.size(); i++)
            // 非常注意,父类对象强制转换成子类对象
            System.out.println(i + 1 + ":" + ((Notice) (noticeList.get(i))).getTitle());

        // 删除第三条公告
        System.out.println("——————删除公告后的内容为:");
        noticeList.remove(2);
        for (int i = 0; i < noticeList.size(); i++)
            // 非常注意,父类对象强制转换成子类对象
            System.out.println(i + 1 + ":" + ((Notice) (noticeList.get(i))).getTitle());

        System.out.println("————————修改后公告:");
        // 修改公告
        notice1.setTitle("修改后公告");
        // 如果这里需要创建新的对象修改则需要调用set方法
        Notice notice5 = new Notice(4, "修改的公告5", "people5", new Date());
        noticeList.set(1, notice5);
        for (int i = 0; i < noticeList.size(); i++)
            // 非常注意,父类对象强制转换成子类对象
            System.out.println(i + 1 + ":" + ((Notice) (noticeList.get(i))).getTitle());

    }
}

测试结果:

1:公告1
2:公告2
3:公告3
——————下面插入添加新公告
1:公告1
2:公告2
3:公告4
4:公告3
——————删除公告后的内容为:
1:公告1
2:公告2
3:公告3
————————修改后公告:
1:修改后公告
2:修改的公告5
3:公告3

猜你喜欢

转载自blog.csdn.net/wankcn/article/details/106909755