List接口

2021.03.12
第32次记录

课堂笔记:
测试List接口中常用方法
1.List集合存储元素特点:有序可重复
有序:List集合中的元素有下标。从0开始,以1递增。
可重复:存储一个1,还可以再存储一个1.
2.List既然是Collection接口中的子接口,那么肯定List接口有自己“特色”的方法。
以下只列出常见的方法:
void add(int index, Object element)
E get(int index)
int indexOf(Object o)
int LastIndexOf(Object o)
Object remove(int index)
Object set(int index, Object element)

代码演示1:

public class ListText01 {
    public static void main(String[] args) {
        //创建List类型的集合
        List myList = new ArrayList();
        //添加元素
        myList.add("A");
        myList.add("B");
        myList.add("C");
        myList.add("D");
        //这个方法使用不多,因为对于ArrayList来说效率比较低
        myList.add(4, "king");
        myList.add(5, "GAME");
        //迭代
        Iterator it = myList.iterator();
        while(it.hasNext()){
            System.out.println(it.next());
        }
    }
}

输出结果:
A
B
C
D
king
GAME

代码演示2:

public class CollectionTest06 {
    public static void main(String[] args) {
        Collection collection = new ArrayList();
        collection.add(1);
        collection.add(2);
        collection.add(3);
        Iterator i = collection.iterator();
        while (i.hasNext()){
            Object obj = i.next();
            i.remove();
            System.out.println(obj);
        }
        System.out.println(collection.size());
    }
}

输出结果:
1
2
3
0

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值