java的ArrayList类

ArrayList<E>E是自定义数据类型

ArrayList类:
构造函数:

 成员方法:
 

public boolean add(E e):

将指定元素加到集合末尾

Appends the specified element to the end of this list.

public class Array {
    public static void main(String[] args) {
        ArrayList arr=new ArrayList();
        arr.add("nihao");//从末尾添加
        arr.add(67);
        arr.add("he");
        System.out.println(arr);//[nihao, 67, he]

    }
}

可以指定向里面特定数据类型

public class Array {
    public static void main(String[] args) {
        ArrayList<String> arr=new ArrayList();
        arr.add("nihao");
        arr.add("eq");
        System.out.println(arr);//[nihao, eq]
    }
}

public void add(int index, E element)

给集合的指定位置插入指定的元素

Inserts the specified element at the specified position in this list. Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices).

public class Array {
    public static void main(String[] args) {
        ArrayList<String> arr=new ArrayList();
        arr.add("nihao");
        arr.add("eq");
        System.out.println(arr);//[nihao, eq]
        arr.add(2,"my");
        System.out.println(arr);//[nihao, eq, my]
    }
}

public E get(int index)

返回索引处的元素

Returns the element at the specified position in this list.

public class Array {
    public static void main(String[] args) {
        ArrayList<String> arr=new ArrayList();
        arr.add("nihao");
        arr.add("eq");
        System.out.println(arr);//[nihao, eq]
        arr.add(2,"my");
        System.out.println(arr);//[nihao, eq, my]
        String el=arr.get(1);
        System.out.println(el);//eq
    }
}

public int size()

返回集合中的集合元素的个数

Returns the number of elements in this list.

public class Array {
    public static void main(String[] args) {
        ArrayList<String> arr=new ArrayList();
        arr.add("nihao");
        arr.add("eq");
        System.out.println(arr);//[nihao, eq]
        arr.add(2,"my");
        System.out.println(arr);//[nihao, eq, my]
        String el=arr.get(1);
        System.out.println(el);//eq
        int size=arr.size();
        System.out.println(size);//3
    }
}

public E remove(int index)

删除指定索引处的元素,返回被删除的元素

Removes the element at the specified position in this list. Shifts any subsequent elements to the left (subtracts one from their indices).

public class Array {
    public static void main(String[] args) {
        ArrayList<String> arr=new ArrayList();
        arr.add("nihao");
        arr.add("eq");
        System.out.println(arr);//[nihao, eq]
        arr.add(2,"my");
        System.out.println(arr);//[nihao, eq, my]
        String el=arr.get(1);
        System.out.println(el);//eq
        int size=arr.size();
        System.out.println(size);//3

        String el2=arr.remove(2);
        System.out.println(el2);//my
        System.out.println(arr);// [nihao, eq]
        
    }
}

public boolean remove(Object o)

删除指定的元素,删除成功返回true,第一个出现的数据

Removes the first occurrence of the specified element from this list, if it is present. If the list does not contain the element, it is unchanged. More formally, removes the element with the lowest index i such that Objects.equals(o, get(i)) (if such an element exists). Returns true if this list contained the specified element (or equivalently, if this list changed as a result of the call).

public class Array {
    public static void main(String[] args) {
        ArrayList<String> arr=new ArrayList();
        arr.add("nihao");
        arr.add("eq");
        System.out.println(arr);//[nihao, eq]
        arr.add(2,"my");
        System.out.println(arr);//[nihao, eq, my]
        String el=arr.get(1);
        System.out.println(el);//eq
        int size=arr.size();
        System.out.println(size);//3

        String el2=arr.remove(2);
        System.out.println(el2);//my
        System.out.println(arr);// [nihao, eq]

        System.out.println(arr.remove("eq"));//true
        System.out.println(arr);//[nihao]

    }
}

public E set(int index, E element)

修改指定索引的值

Replaces the element at the specified position in this list with the specified element.

         arr.set(0,"hhh");
         System.out.println(arr);//[hhh]

从集合中遍历元素,删除含有特定内容的元素,应该怎么做:

方法一:每次删除一个元素,索引-1;

方法二:从集合的最后开始向前遍历,可以避免漏掉元素

  • 28
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

落落落sss

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值