2024年java基础知识列表

java基础知识列表一 List 了解 一 List 列表 有序的 Collection 允许重复元素 1 2 4 5 2 1 3 二 List 主要实现 同步 非同步 针对线程而言 ArrayList 非同步的 LinkedList 非同步 Vector 同步 二 ArrayList 数组实现 一 ArrayList 了解 以数组实现的列表 不支持同步

大家好,我是讯享网,很高兴认识大家。



一:List了解

(一)List:列表

–有序的Collection –允许重复元素 –{124,{52},13}
讯享网

(二)List:主要实现

同步/非同步:针对线程而言

讯享网–ArrayList(非同步的) –LinkedList(非同步) –Vector(同步)  

二:ArrayList:数组实现

(一)ArrayList了解

–以数组实现的列表,不支持同步 List list = Collections.synchronizedList(new ArrayList(...)); –利用索引位置可以快速定位访问–不适合指定位置的插入、删除操作 –适合变动不大,主要用于查询的数据 –和Java数组相比,其容量是可动态调整的 –ArrayList在元素填满容器时会自动扩充容器大小的50%

(二)ArrayList实现

讯享网 ArrayList<Integer> al = new ArrayList<Integer>(); //<>是泛型编程,类似于C++模板 al.add(3); al.add(1); al.add(2); al.add(9); al.add(6); al.add(new Integer(6)); System.out.println("the third elem:"+al.get(2)); //从0开始索引 al.remove(3); //删除第四个元素 al.add(3, 10); //插入第四个元素为10
 //进行遍历  trverseByIterator(al); trverseByIndex(al); trverseByFor(al);
 public static void trverseByIterator(ArrayList<Integer> al) { //使用迭代器遍历 System.out.println("==========迭代器遍历==========="); long startTime = System.nanoTime(); //获取开始时间,以纳秒为单位返回正在运行的Java虚拟机的高分辨率时间源的当前值。  Iterator<Integer> iter = al.iterator(); //获取迭代指针 while(iter.hasNext()) { System.out.print(iter.next());  //指针会下移,但是会先返回当前指针指向的数据值 } long endTime = System.nanoTime(); long duration = endTime-startTime; System.out.println("iterator使用纳秒:"+duration); } public static void trverseByIndex(ArrayList<Integer> al) { //使用迭代器遍历 System.out.println("==========index索引遍历==========="); long startTime = System.nanoTime(); //获取开始时间,以纳秒为单位返回正在运行的Java虚拟机的高分辨率时间源的当前值。  for(int i=0;i<al.size();i++) { System.out.print(al.get(i)); } long endTime = System.nanoTime(); //获取开始时间,以纳秒为单位返回正在运行的Java虚拟机的高分辨率时间源的当前值。  long duration = endTime-startTime; System.out.println("index使用纳秒:"+duration); } public static void trverseByFor(ArrayList<Integer> al) { //使用迭代器遍历 System.out.println("==========for索引遍历==========="); long startTime = System.nanoTime(); //获取开始时间,以纳秒为单位返回正在运行的Java虚拟机的高分辨率时间源的当前值。  for(Integer item : al) { System.out.print(item); } long endTime = System.nanoTime(); //获取开始时间,以纳秒为单位返回正在运行的Java虚拟机的高分辨率时间源的当前值。  long duration = endTime-startTime; System.out.println("for使用纳秒:"+duration); }
==========迭代器遍历=========== iterator使用纳秒: ==========index索引遍历=========== index使用纳秒: ==========for索引遍历=========== for使用纳秒:
输出

(三)性能测试:索引效率高

 for(int i= java基础知识列表0;i<10000;i++) { al.add(i); }
==========迭代器遍历=========== iterator使用纳秒: ==========index索引遍历=========== index使用纳秒: ==========for索引遍历=========== for使用纳秒:

三:LinkedList:链表实现

(一)LinkedList了解

–以双向链表实现的列表,不支持同步 List list = Collections.synchronizedList(new LinkedList(...)); –可被当作堆栈、队列和双端队列进行操作 –顺序访问高效,随机访问较差,中间插入和删除高效 –适用于经常变化的数

(二)LinkedList实现

public class LinkedListTest { public static void main(String[] args) { LinkedList<Integer> ll = new LinkedList<Integer>(); //<>是泛型编程,类似于C++模板 /*for(int i=0;i<10000;i++) { ll.add(i); } */ ll.add(3); ll.add(1); ll.add(2); ll.add(9); ll.add(6); ll.add(new Integer(6)); System.out.println("the third elem:"+ll.get(2)); //从0开始索引 ll.remove(3); //删除第四个元素 ll.add(3, 10); //插入第四个元素为10 //进行遍历  trverseByIterator(ll); trverseByIndex(ll); trverseByFor(ll); } public static void trverseByIterator(LinkedList<Integer> ll) { //使用迭代器遍历 System.out.println("==========迭代器遍历==========="); long startTime = System.nanoTime(); //获取开始时间,以纳秒为单位返回正在运行的Java虚拟机的高分辨率时间源的当前值。  Iterator<Integer> iter = ll.iterator(); //获取迭代指针 while(iter.hasNext()) { System.out.print(iter.next()); } long endTime = System.nanoTime(); long duration = endTime-startTime; System.out.println("iterator使用纳秒:"+duration); } public static void trverseByIndex(LinkedList<Integer> ll) { //使用迭代器遍历 System.out.println("==========index索引遍历==========="); long startTime = System.nanoTime(); //获取开始时间,以纳秒为单位返回正在运行的Java虚拟机的高分辨率时间源的当前值。  for(int i=0;i<ll.size();i++) { System.out.print(ll.get(i)); } long endTime = System.nanoTime(); //获取开始时间,以纳秒为单位返回正在运行的Java虚拟机的高分辨率时间源的当前值。  long duration = endTime-startTime; System.out.println("index使用纳秒:"+duration); } public static void trverseByFor(LinkedList<Integer> ll) { //使用迭代器遍历 System.out.println("==========for索引遍历==========="); long startTime = System.nanoTime(); //获取开始时间,以纳秒为单位返回正在运行的Java虚拟机的高分辨率时间源的当前值。  for(Integer item : ll) { System.out.print(item); } long endTime = System.nanoTime(); //获取开始时间,以纳秒为单位返回正在运行的Java虚拟机的高分辨率时间源的当前值。  long duration = endTime-startTime; System.out.println("for使用纳秒:"+duration); } }
==========迭代器遍历=========== iterator使用纳秒: ==========index索引遍历=========== index使用纳秒: ==========for索引遍历=========== for使用纳秒:
输出

(三)性能测试:for遍历效率高

==========迭代器遍历=========== iterator使用纳秒: ==========index索引遍历=========== index使用纳秒: ==========for索引遍历=========== for使用纳秒:

四:vector向量(同步)

(一)vector了解

–和ArrayList类似,可变数组实现的列表 –Vector同步,适合在多线程下使用 –原先不属于JCF框架,属于Java最早的数据结构,性能较差 –从JDK1.2开始,Vector被重写,并纳入到JCF –官方文档建议在非同步情况下,优先采用ArrayList

(二)vector实现

import java.util.Vector; import java.util.Iterator; public class vectorTest { public static void main(String[] args) { Vector<Integer> v = new Vector<Integer>(); //<>是泛型编程,类似于C++模板 for(int i=0;i<10000;i++) { v.add(i); } //进行遍历  trverseByIterator(v); trverseByIndex(v); trverseByFor(v); } public static void trverseByIterator(Vector<Integer> v) { //使用迭代器遍历 System.out.println("==========迭代器遍历==========="); long startTime = System.nanoTime(); //获取开始时间,以纳秒为单位返回正在运行的Java虚拟机的高分辨率时间源的当前值。  Iterator<Integer> iter = v.iterator(); //获取迭代指针 while(iter.hasNext()) { iter.next(); } long endTime = System.nanoTime(); long duration = endTime-startTime; System.out.println("iterator使用纳秒:"+duration); } public static void trverseByIndex(Vector<Integer> v) { //使用迭代器遍历 System.out.println("==========index索引遍历==========="); long startTime = System.nanoTime(); //获取开始时间,以纳秒为单位返回正在运行的Java虚拟机的高分辨率时间源的当前值。  for(int i=0;i<v.size();i++) { v.get(i); } long endTime = System.nanoTime(); //获取开始时间,以纳秒为单位返回正在运行的Java虚拟机的高分辨率时间源的当前值。  long duration = endTime-startTime; System.out.println("index使用纳秒:"+duration); } public static void trverseByFor(Vector<Integer> v) { //使用迭代器遍历 System.out.println("==========for索引遍历==========="); long startTime = System.nanoTime(); //获取开始时间,以纳秒为单位返回正在运行的Java虚拟机的高分辨率时间源的当前值。  for(Integer item : v) { ; } long endTime = System.nanoTime(); //获取开始时间,以纳秒为单位返回正在运行的Java虚拟机的高分辨率时间源的当前值。  long duration = endTime-startTime; System.out.println("for使用纳秒:"+duration); } }
View Code

(三)性能测试:for效率好

五:总结

(一)ArrayList/LinkedList/Vector

(二)同步采用Vector

(三)非同步情况下,根据数据操作特点选取ArrayList/LinkedList

(四)ArrayList与LinkedList插入,查找性能比较

ArrayList查找效率高,LinkedList查找效率低。(修改同查找) ArrayList删除效率低,需要移动大量数据,LinkedList删除效率高,不需要移动数据。<视情况而论,毕竟LinkedList会先进行查找操作>
小讯
上一篇 2024-12-24 09:15
下一篇 2024-12-28 20:30

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/512.html