集合框架(1)——ArrayList

Collection接口 有两个子接口 List 和 Set

List接口特点: 存储的元素是有序可重复的

Set接口特点: 存储的元素是无序不可重复的

一、ArrayList集合

ArrayList类中封装了一个数组,存储的元素都是存在封装的数组中。

实现ArrayList类的源代码

1. 定义MyArrayList中的方法

public class MyArrayList {
    public int size() {
        // TODO Auto-generated method stub
        return 0;
    }
    public boolean contains(Object o) {
        // TODO Auto-generated method stub
        return false;
    }
    public boolean add(Object e) {
        // TODO Auto-generated method stub
        return false;
    }
    public void add(int index,Object e) {
    }
    public Object set(int index,Object e) {
        return null;
    }
    public boolean remove(Object o) {
        // TODO Auto-generated method stub
        return false;
    }
    public Object remove(int index) {
        // TODO Auto-generated method stub

        return null;
    }
    public void clear() {
        // TODO Auto-generated method stub
    }
    public Object get(int index) {
        return null;
    }
}

2. 定义MyArrayList中的属性:

Object数组属性,存储元素的

size属性, 是一个索引位置,元素的操作范围 0 ~ size-1

容量属性

3. 构造器

/**
* 无参构造器创建的集合容量默认是10
*/
public MyArrayList() {
    objs = new Object[initialCapacity];
}
/**
* 指定初始容量,封装数组的长度,由于创建的集合可能要存储大量的数据
* 直接给一个大的容量,就不用经历多次扩容
* @param initialCapacity
*/
public MyArrayList(int initialCapacity) {
    this.initialCapacity = initialCapacity;
    objs = new Object[initialCapacity];
}

4. 实现size方法、get方法

/**
* 根据指定的索引获得 存储的元素
* @param index 封装的数组中的索引
* @return Object类型的对象(多态)
*/
public Object get(int index) {
    if(index<0||index>=size) {
        throw new IndexOutOfBoundsException();
    }
        return objs[index];
}

/**
* 获得集合中的元素个数
* @return
*/
public int size() {
    return size;
}

5. 实现add方法

/**
* 向集合中存储一个对象,向末尾添加一个元素
* @param e 存储的对象
* @return 插入成功返回true
*/
public boolean add(Object e) {
    //判断数组是否要扩容
    if(size>=objs.length-1) {
        //扩容为50%
        //创建新数组,拷贝,再引用给objs
        addCapacity();
    }
    //元素添加到size位置,size后移
    objs[size++]=e;
    return true;
    }
/**
* 扩容的方法,源码中要用扩容的长度和默认容量10 比较,如果小于10 扩容为10
*/
    private void addCapacity() {
        objs = Arrays.copyOf(objs, objs.length+(objs.length>>1)+1);
    }
/**
* 向指定位置添加元素
* @param index
* @param e
*/
public void add(int index,Object e) {
    if(index<0||index>=size) {
        throw new IndexOutOfBoundsException();
    }
        addCapacity();
        //指定位置的元素向后移动一位
        //将插入的元素 放到index位置
    for(int i = size-1;i>=index;i--) {
        objs[i+1]=objs[i];
    }
    objs[index] = e;

    size++;
}

6. set方法

/**
* 将指定位置的元素 替换成指定的元素
* @param index
* @param e
* @return 返回修改前的那个对象
*/
public Object set(int index,Object e) {
    if(index<0||index>=size) {
        throw new IndexOutOfBoundsException();
    }
        Object temp = objs[index];
        objs[index] = e;
        return temp;
}

二、ArrayList应用

public class Test {
    public static void main(String[] args) {
        ArrayList list = new ArrayList();
//remove
        list.add(1);
        list.add(3);
        list.add(5);
        list.add(7);
        list.add(9);
        list.add(3);
        list.add(10);
//list.remove(1); //删除的是索引1 ? 还是 Integer(1) :因为有remove(int i)方法
        list.remove(new Integer(3)); //执行remove(Object obj) 方法
        System.out.println(list);
    }
}

 

public class Test {
    public static void main(String[] args) {
        ArrayList list = new ArrayList();
        list.add(new Person("101","tom1",21));
        list.add(new Person("102","tom3",23));
        list.add(new Person("103","tom5",25));
        list.add(new Person("104","tom2",22));
        list.add(new Person("105","tom4",24));
    //删除身份证是103的人
    //方式一: 迭代判断 找出身份是103人的 索引位置 remove(索引)
    /* for(int i = 0;i<list.size();i++) {
    //p对每次循环取出的person对象 进行引用
    Person p = (Person)list.get(i);
    if("103".equals(p.getSfz())) {
    list.remove(i);
    break;
    }
}*/
    //方式二:remove(对象)方式,重写Person类的equals方法,两个人的sfz相同返回true
    //list.remove(new Person("103","tom5",25));
        System.out.println(list);
        System.out.println(list.contains(new Person("103","tom5",25)));
    }    
}
public class Person {
    private String sfz;
    private String name;
    private int age;
    public String getSfz() {
        return sfz;
    }
    public void setSfz(String sfz) {
        this.sfz = sfz;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public Person(String sfz, String name, int age) {
        super();
        this.sfz = sfz;
        this.name = name;
        this.age = age;
    }
    @Override
    public boolean equals(Object obj) {
        if(this==obj) return true;
        if(obj instanceof Person) {
            Person p = (Person)obj;
            if(this.sfz.equals(p.sfz))
                return true;
        }
        return false;
    }
    @Override
    public String toString() {
        return name;
    }
}

三、ArrayList的优缺点总结:

插入、删除元素时效率低,遍历元素效率高

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

aigo-2021

您的鼓励是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值