当前位置: 首页 > news >正文

java attributelist_java集合类(二)List学习

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 subList(int fromindex,int toindex)

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

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

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

8)其他:int hashcode()

AboutArrayList:

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

实例代码:

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 alist = new ArrayList(Arrays.asList(1,2,3));

//3)Constructs an empty list with the specified initial capacity

ArrayList 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();

}

AboutLinkedList:

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

public void linkedlist(){

LinkedList linklist = new LinkedList();

ArrayList 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 a = new ArrayList();

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 l = new LinkedList();

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);

}

输出:

a0d0a99fba976c158549292e2d78c8db.png

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

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

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

相关文章:

  • java ccf认证解答_CCF认证考试(java)---窗口
  • java 平滑 停止_设计Java应用程序的平滑停止
  • java开发按键精灵_Java 按键精灵
  • java同步开销_java – 为什么对Map的同步访问会增加大量的开销
  • java字节码常量池_Java字节码常量池深度剖析与字节码整体结构分解
  • java控制语句案例_Java基础之流程控制(示例代码)
  • mysql 怎么设置ip地址_Mysql如何设置用户指定ip地址操作数据库
  • android_iphone和java三个平台一致的加密方法_Android、iPhone和Java三个平台一致的加密工具...
  • java最崇拜谁_蓝桥杯(java)个人赛真题:小朋友崇拜圈
  • java数组函数结局实际问题_java函数与数组
  • java登录字符串封装_JAVA的随机的字符串的封装(基本上够用了)
  • java统一管理文字_为了统一管理组件和容器,Java为所有组件类定义的超类
  • django mysql 分表_Django数据库分表
  • php aws_Amazon S3 客户端加密与 AWS SDK for PHP 版本 3 - 适用于 PHP 的 AWS 开发工具包...
  • Php公钥加密data是空,实用的PHP带公钥加密类分享(每次加密结果都不一样哦)
  • 实现windows 窗体的自己画,网上摘抄的,学习了
  • 【跃迁之路】【477天】刻意练习系列236(2018.05.28)
  • Angular 响应式表单之下拉框
  • Effective Java 笔记(一)
  • ES6 ...操作符
  • js ES6 求数组的交集,并集,还有差集
  • Otto开发初探——微服务依赖管理新利器
  • php的插入排序,通过双层for循环
  • Vue实战(四)登录/注册页的实现
  • 阿里研究院入选中国企业智库系统影响力榜
  • 简单易用的leetcode开发测试工具(npm)
  • 模仿 Go Sort 排序接口实现的自定义排序
  • 目录与文件属性:编写ls
  • 批量截取pdf文件
  • 如何进阶一名有竞争力的程序员?
  • 实战|智能家居行业移动应用性能分析
  • 一起来学SpringBoot | 第三篇:SpringBoot日志配置
  • elasticsearch-head插件安装
  • ionic异常记录
  • ​​​​​​​​​​​​​​Γ函数
  • ​LeetCode解法汇总518. 零钱兑换 II
  • ​RecSys 2022 | 面向人岗匹配的双向选择偏好建模
  • ​二进制运算符:(与运算)、|(或运算)、~(取反运算)、^(异或运算)、位移运算符​
  • (39)STM32——FLASH闪存
  • (附源码)计算机毕业设计SSM教师教学质量评价系统
  • (轉貼)《OOD启思录》:61条面向对象设计的经验原则 (OO)
  • ./configure、make、make install 命令
  • .describe() python_Python-Win32com-Excel
  • .NET Framework与.NET Framework SDK有什么不同?
  • .NET Micro Framework初体验(二)
  • .net 按比例显示图片的缩略图
  • .net 生成二级域名
  • .NET/ASP.NETMVC 大型站点架构设计—迁移Model元数据设置项(自定义元数据提供程序)...
  • .NET/C# 使用 #if 和 Conditional 特性来按条件编译代码的不同原理和适用场景
  • .NET6 开发一个检查某些状态持续多长时间的类
  • ??javascript里的变量问题
  • @Conditional注解详解
  • @for /l %i in (1,1,10) do md %i 批处理自动建立目录
  • @SuppressLint(NewApi)和@TargetApi()的区别
  • [.net 面向对象程序设计进阶] (19) 异步(Asynchronous) 使用异步创建快速响应和可伸缩性的应用程序...