java集合类(二)List学习

2023-05-16

     接上篇  java集合类(一)

  1. List接口继承了Collection接口和Iterable接口,即同样含有Collection和 Iterable的特性,还有方法,其基本方法有

    1)有关添加: boolean add(E e):添加元素   void add(int index,E element):在特定位置添加元素        

    boolean addAll(Collection<? extends E> c):添加集合中所有的元素    boolean addAll(int index,Collection<? extends E> c):在特定位置添加一组元素

    2)有关清除:void clear(), E remove(int index),boolean remove(Object o),boolean remove(Collection<?> c)

    3)有关检验:boolean contains(Object o),boolean containAll(Collection<?> c),boolean equals(Object o),boolean isEmpty()

    4)有关获取:E get(int index),int hashCode(),int indexOf(Object o),int lastIndexOf(Object o),int size(),Lisy<E> subList(int fromindex,int toindex)

    5)有关设定:E set(int index, E element),boolean retainAll(Collection<?> c)

    6)有关打印:Object[] toArray(),    <T>T[] toArray(T[] a)    

    7)有关迭代:Iterator<E> iterator(),ListIterator<E> listIterator(),ListIterator<E> listIterator(int index)

    8)其他:int hashcode()

       List接口的实现类有很多,如:AbstractList, AbstractSequentialList, ArrayList, AttributeList, CopyOnWriteArrayList, LinkedList, RoleList,

      RoleUnresolvedList,  Stack, Vector,下面介绍List的几个基本实现类的用法:

  • About ArrayList:

    All Implemented Interfaces:  Serializable, Cloneable, Iterable<E>, Collection<E>, List<E>, RandomAccess extends AbstractList

    Direct Known Subclasses:  AttributeList, RoleList, RoleUnresolvedList

    实例代码:

class iphone{} 
	
public void arraylist(){
	//1)Constructs an empty list with an initial capacity of ten
	ArrayList al = new ArrayList();
	/*
	 * 2)Constructs a list containing the elements of the specified 
	 * collection, in the order they are returned by the collection's iterator
	*/
	ArrayList<Integer> alist = new ArrayList(Arrays.asList(1,2,3));
	//3)Constructs an empty list with the specified initial capacity
	ArrayList<Double> alist1 = new ArrayList(10);
	al.add(new iphone());
	al.add(1);
	al.add("a");
	al.add(alist);
	System.out.println(al.hashCode()); //int hashcode()
		
	/*
	 * usage of methods that aren't mentioned within "List basic methods"
	 */
	//ensure that list can hold at least the number of elements specified by the minimum capacity argument
	alist1.ensureCapacity(30);
	//Trims the capacity of this ArrayList instance to be the list's current size
	alist.trimToSize();
        
        //Returns a shallow copy of this ArrayList instance(The elements themselves are not copied)
        Object o = al.clone();
}
  • About LinkedList:  

     All Implemented Interfaces:Serializable, Cloneable, Iterable<E>, Collection<E>, Deque<E>, List<E>, Queue<E>  extends AbstractSequentialList<E>

     从上面可以知道,由于LinkedList实现的接口涉及到队列,所以它也会新增一些有关队列独有操作方法,还有pop(),push()等,下面举例:

public void linkedlist(){
	LinkedList linklist = new LinkedList();
	ArrayList<Integer> a = new ArrayList(Arrays.asList(1,2,3));
	LinkedList linklist2 = new LinkedList(a);
		
	Iterator i = linklist2.iterator();
	while(i.hasNext()){
		System.out.println(i.next());   //1  2  3
	}
	Iterator ri = linklist2.descendingIterator();
	while(ri.hasNext()){
		System.out.println(ri.next());   //3  2  1
	}
		
	linklist.addFirst(1);
	linklist.addLast("e");
	System.out.println(linklist);  //[1,e]  
	/*linklist.getFirst();
	linklist.getLast();*/
		
	//Retrieves, but does not remove, the head (first element) of this list
	System.out.println(linklist.element());  //1
		
	linklist.offer("x");
	System.out.println(linklist);//[1,e,x]
	linklist.offerFirst(0);
	linklist.offerLast("y");
	System.out.println(linklist);//[0,1,e,x,y]
		
	//peek..()--->[Retrieves, but does not remove]
	System.out.println(linklist.peek());//0
	System.out.println(linklist.peekFirst());//0 ,list is empty,return "null"
	System.out.println(linklist.peekLast());//y,list is empty, return "null"
		
	//poll..()--->[Retrieves and removes]
	System.out.println(linklist.poll()); //0
	System.out.println(linklist.pollFirst());//1
	System.out.println(linklist.pollLast()); //y
	System.out.println(linklist);//[e,x]
		
	/*linklist.removeFirst();    //return first element
	linklist.removeFirstOccurrence(Object);   //boolean
	linklist.removeLast();   //return last element
	linklist.removeLastOccurrence(Object);  //boolean
	*/ 
		
	System.out.println(linklist2); //[1  2  3]
	//Pops an element from the stack represented by this list
	System.out.println(linklist2.pop()); //1
	//Pushes an element onto the stack represented by this list
	linklist2.push("m"); 
	System.out.println(linklist2);//[m,2,3]
	}
  • ArrayList 和LinkedList的比较:

     1.ArrayList是基于动态数组,LinkedList基于链表
       2.对于随机访问get和set,ArrayList觉得优于LinkedList,因为LinkedList要移动指针
       3.对于新增和删除操作add和remove,LinedList比较占优势,因为ArrayList要移动数据

       验证:

public void comparearraylistwithlinklist(){
	SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd  hh:mm:ss");
	System.out.println(f.format(new Date()));
	    
	ArrayList<Integer> a = new ArrayList<Integer>();
	long starttime = new Date().getTime();
	for(int i = 0; i<10000; i++){
		a.add(i);
	}
	long finishtime = new Date().getTime();
	System.out.println(finishtime-starttime);
		
	LinkedList<Integer> l = new LinkedList<Integer>();
	long lstarttime = new Date().getTime();
	for(int i = 0; i<10000; i++){
		l.add(i);
	}
	long lfinishtime = new Date().getTime();
	System.out.println(lfinishtime-lstarttime);
}

  输出:

      

  额外说明:以上验证代码是基于较大量数据的,输出也是不稳定的,即答案也不能确定,可能是我用错测试方法,也可能是因为数据量不够大,也可能是因为getTime()获得的是毫秒,程序可能需要更精确的时间单位,这样才有办法比较。另外,如果对于单个数据的插入或删除,是不是LinkedList还优于ArrayList呢?答案也很明显是不一定的,读者可以按照上面的实例验证一下

由于此博文可能有点长了,其他List的学习见“java集合类(三)List学习(续)”,尽请期待!!

 

###    学习从来都是一个过程,对对错错对对...若文中有错误,还望读者批评指出      ###

 

转载于:https://www.cnblogs.com/allenpengyu/p/3460453.html

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

java集合类(二)List学习 的相关文章

随机推荐

  • matlab练习程序(图像水平/竖直移动)

    cl delta x 61 67 要求为整数 xff0c 水平移动的偏移量 xff0c 正为向右 xff0c 负为向左 delta y 61 89 要求为整数 xff0c 竖直移动的偏移量 xff0c 正为向下 xff0c 负为向上 img
  • 第二章 M文件程序设计(matlab)

    Contents 第二章 M文件程序设计M文件一 程序控制结构1 顺序结构1 数据的输入input2 数据的输出disp 输出字符串或者矩阵3 程序的暂停 pause Ctrl 43 C 延迟的秒数 2 选择结构1 IF语句2 switch
  • 无人机二次开发硬件清单

    四旋翼系统 3D Robotics IRIS 43 飞控 功能组件 eBumper4 声呐避障模块 组合功能 DJI Matrice100 43 Guidance 43 Onboard SDK
  • npm install报错解决方法ls-remote -h -t git://github.com/adobe-webplatform/eve.git

    npm install 时报错 xff0c 使用ssh 和 https 重新克隆代码都不行 xff0c 以下是解决办法 npm ERR usr local bin git ls remote h t git github com adobe
  • reStructuredText文件语法简单学习

    reStructuredText 是一种扩展名为 rst的纯文本文件 xff0c 通过特定的解释器 xff0c 能够将文本中的内容输出为特定的格式 1 章节标题 章节头部由下线 xff08 也可有上线 xff09 和包含标点的标题组合创建
  • [Hadoop 周边] 浅谈大数据(hadoop)和移动开发(Android、IOS)开发前景【转】

    原文链接 xff1a http www d1net com bigdata news 345893 html 先简单的做个自我介绍 xff0c 我是云6期的 xff0c 黑马相比其它培训机构的好偶就不在这里说 xff0c 想比大家都比我清楚
  • 无代理防毒:虚拟化与安全的深度融合

    当今 xff0c 用户对虚拟化和云计算平台的需求正在呈现爆发趋势 然而 xff0c 虚拟化技术其实是一把双刃剑 xff0c 它具有更高效率地利用计算 存储资源等优势 xff0c 但这些优势都依赖于虚拟化系统资源高度集中的特性 xff0c 但
  • 计算机专业找工作注意什么

    为什么80 的码农都做不了架构师 xff1f gt gt gt http www java sh Sharing qzjq 1722 html 几年前市场对计算机专业人才的需求非常大 xff0c 计算机专业红得发紫 xff0c 毕业生是 皇
  • 解析Linux C/C++开发的难度到底在哪

    解析Linux C C 43 43 开发的难度到底在哪 经常被问到的问题 如何学习C 43 43 如何学习Linux 如何开始 xff1f 其实这个问题对于不同的人答案往往不尽相同 xff0c 每个人所处的环境就不同 一个普遍的现象是有一些
  • 在局域网内实现https安全访问

    在局域网内实现https安全访问 准备原料 服务器 resin xff08 当然也可以是tomcat xff0c 这里以resin为例 xff09 安装jdk 域名 xff08 随便写一个就行 xff0c 因为是内网使用 xff0c 不会被
  • GWAS | 全基因组关联分析 | PLINK | 实战 | 统计遗传学

    参考 xff1a PLINK File format reference vcftools plink的主要功能 xff1a 数据处理 xff0c 质量控制的基本统计 xff0c 群体分层分析 xff0c 单位点的基本关联分析 xff0c
  • 七周七语言——Prolog(二)

    1 递归 首先来看一个知识库 xff1a father zeb john boy sr father john boy sr john boy jr ancestor X Y father X Y ancestor X Y father X
  • 上传图片到阿里云OSS和获取上传图片的外网url的步骤

    啥都不说 直接上代码 1 html lt form action 61 34 bcis api headImgUpload json 34 method 61 34 post 34 enctype 61 34 multipart form
  • 如何提取到网页上播放的视频

    1 在播放视频页面的空白处点击右键 xff0c 会弹出菜单 xff0c 在菜单中选择审查元素 2 弹出的页面如图所示 xff0c 我们点击第二个network 3 利用size这一栏点击一下他就会按照文件大小就可以查找到视频 xff0c 一
  • Nodejs如何调用Dll模块

    苏格团队作者 xff1a Tomey 一 为什么需要用node js调用dll xff1f 公司项目采用Electron xff08 electronjs org xff09 开发pc应用 xff0c 会涉及到与底层硬件设备的通信 xff0
  • PyShark入门(1):简介

    原文地址 xff1a http zodiacg net 2016 07 in 本系列文章译自thePacketGeek的系列文章 原创翻译 xff0c 转载请注明出处 文章作者以PyShark为基础开发了Cloud Pcap xff0c 一
  • 将元素添加到List集合的第一位

    2019独角兽企业重金招聘Python工程师标准 gt gt gt list add 1 object 看一下add方法的注释 Inserts the specified element at the specified position
  • KMS激活时,常见的错误及处理

    0x80072EFE 出现这种是错误因为服务器响应超时或与服务器连接已丢失 xff0c 网络连接的问题 解决方法 xff1a 暂时关闭防火墙和杀毒软件 xff0c 将网卡禁用再启用 xff0c 确保网络是通的 0x8007232B 过 30
  • 精彩网页

    新华网美国频道 xff1a http us xinhuanet com php加密网址 xff1a https www phpjiami com 转载于 https www cnblogs com net5x p 6570727 html
  • java集合类(二)List学习

    接上篇 java集合类 xff08 一 xff09 List接口继承了Collection接口和Iterable接口 xff0c 即同样含有Collection和 Iterable的特性 xff0c 还有方法 xff0c 其基本方法有 xf