欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 文旅 > 八卦 > List接口, ArrayList Vector LinkedList

List接口, ArrayList Vector LinkedList

2024/10/24 3:26:58 来源:https://blog.csdn.net/qq_64633597/article/details/140067906  浏览:    关键词:List接口, ArrayList Vector LinkedList

Collection接口的子接口

子类Vector,ArrayList,LinkedList

1.元素的添加顺序和取出顺序一致,且可重复

2.每个元素都有其对应的顺序索引

方法

  1. 在index = 1 的位置插入一个对象,list.add(1,list2)
  2. 获取指定index位置的元素,Object get(int index)
  3. 返回obj在集合中首次出现的位置,int indexOf(Object obj)
  4. 返回最后出现的位置    int lastIndexOf( Object obj )
  5. 移除指定index位置的元素,并返回 Object remove (int index)
  6. 用ele替换指定index位置的元素 Object set (int index ,Object ele)
  7. 返回fromIndex 到toIndex位置的子集合, List sublist(int fromIndex, int toIndex)

遍历方式

1.Iterator

2.增强for循环

3.普通for循环

package chapter;import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;public class noStatue {public static void main(String[] args) {@SuppressWarnings({"all"})List list = new ArrayList();list.add("jack");list.add("wow");//迭代器Iterator iterator = list.iterator();while (iterator.hasNext()) {Object next =  iterator.next();System.out.println(next);}//增强forfor (Object o :list) {System.out.println(o);}//普通for循环for (int i = 0; i < list.size(); i++) {System.out.println(list.get(i));}}
}

ArrayList

  1. ArrayList可以加入多个null
  2. 由数组实现数据存储
  3. 基本等同于Vector,但ArrayList,执行效率高,线程不安全,多线程不建议使用
  4. 底层为对象数组

底层结构与源码分析

p 511

无参构造器,初始容量为0,第一次增加为10,在扩容,则扩容到1.5倍

指定大小的构造器,初始容量为指定,扩容后为1.5倍

Vector

底层也是对象数组,线程同步(安全)

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com