list列表

list可以看成是变长的动态数组,其元素以线性方式存储,集合中可以存放重复对象。
    List接口主要实现类包括:
    ArrayList() : 代表长度可以改变得数组。可以对元素进行随机的访问,向ArrayList()中插入与
    与删除元素的速度慢。
    LinkedList(): 在实现中采用链表数据结构。插入和删除速度快,访问速度慢。
    对于List的随机访问来说,就是只随机来检索位于特定位置的元素。
    List 的 get(int index) 方法放回集合中由参数index指定的索引位置的对象,下标从“0” 开始

继承自collection接口,实现的方法

 boolean add(Object o)   :向集合中加入一个对象的引用
    void clear()                        :删除集合中所有的对象,即不再持有这些对象的引用
    boolean isEmpty()           :判断集合是否为空
    boolean contains(Object o): 判断集合中是否持有特定对象的引用
    Iterartor iterator()              : 返回一个Iterator对象,可以用来遍历集合中的元素
    boolean remove(Object o):从集合中删除一个对象的引用
    int size()                               :返回集合中元素的数目
    Object[] toArray()                 :返回一个数组,该数组中包括集合中的所有元素

 Iterator接口声明了如下方法:
    hasNext(): 判断集合中元素是否遍历完毕,如果没有,就返回true
    next()       :返回下一个元素
    remove():从集合中删除上一个有next()方法返回的元素。

声明与初始化ArrayList<类型> array = new ArrayList<类型>() 

 获得迭代器:  Iterator<类型> it = array. iterator(); 

例子

ArrayList<String> al = new ArrayList<String>();
al.add("Java");
al.add("C++");
al.add("Pascal");
al.add("Perl");
//Iterator<String> it = al.iterator();
Iterator it = al.iterator();
while(it.hasNext()){
//String str = it.next().toString();
String str = it.next();
System.out.println("Item :"+str);
}
for(it=al.iterator();it.hasNext();){
String str = it.next().toString();
System.out.println("remove :"+str);
it.remove();
}

转载于:https://www.cnblogs.com/lxy1998/p/6775724.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值