数组与列表的随记——JAVA

目录

一、数组

1.无需索引的遍历

2.数组初始化定义

二、列表

ArraryList:非同步

LinkedList:

Vector:


 

一、数组

1.无需索引的遍历

//d为一维数组
for(int e : d) {
			System.out.println(e);
		}
//a为二维数组
for(int[] items : a)
		{
			for(int item : items)
			{
				System.out.print(item + ", ");
			}
			System.out.println();
		}

2.数组初始化定义

正确方法:

int a[]; //a 还没有new操作  实际上是null,也不知道内存位置
		int[] b; //b 还没有new操作  实际上是null,也不知道内存位置
		int[] c = new int[2]; //c有2个元素,都是0
		c[0] = 10; c[1] = 20; //逐个初始化
		
		int d[] = new int[]{0,2,4};//d有3个元素, 0,2,4,同时定义和初始化
		int d1[] = {1,3,5};        //d1有3个元素, 1,3,5 同时定义和初始化

错误方法:

	注意声明变量时候没有分配内存,不需要指定大小,以下是错误示例
		int e[5];
		int[5] f;
		int[5] g = new int[5];
		int h[5] = new int[5];

二、列表

list是有序的(此处有序指的是List 是按照元素的添加顺序来存储的,附Set无序指Set 的实现类都有一套自己的排序算法,每添加一个元素,都会按照其内部算法将元素添加到合适的位置,所以不能保证内部存储是按元素添加的顺序而存储的)

ArraryList:非同步

LinkedList:

非同步,最好不要用随机索引遍历,速度慢。

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;

public class LinkedListTest {

	public static void main(String[] args) {
		LinkedList<Integer> ll = new LinkedList<Integer>();  
	    ll.add(3);  
	    ll.add(2);  
	    ll.add(5);  
	    ll.add(6);  
	    ll.add(6);  
	    System.out.println(ll.size());
	    ll.addFirst(9);  //在头部增加9
	    ll.add(3, 10);   //将10插入到第四个元素,四以及后续的元素往后挪动
	    ll.remove(3);    //将第四个元素删除
	    
	    LinkedList<Integer> list = new LinkedList<Integer>();
	    for (int i=0; i<100000; i++)
	    {
	    	list.add(i);
	    }
	    traverseByIterator(list);
	    traverseByIndex(list);
	    traverseByFor(list);    

	}
	
	public static void traverseByIterator(LinkedList<Integer> list)
	{
		long startTime = System.nanoTime();
		System.out.println("============迭代器遍历=============="); 
	    Iterator<Integer> iter1 = list.iterator();  
	    while(iter1.hasNext()){  
	        iter1.next();  
	    }
		long endTime = System.nanoTime();
	    long duration = endTime - startTime;
	    System.out.println(duration + "纳秒");
	}
	public static void traverseByIndex(LinkedList<Integer> list)
	{
		long startTime = System.nanoTime();
		System.out.println("============随机索引值遍历=============="); 
	    for(int i=0;i<list.size();i++)
	    {
	    	list.get(i);
	    }
		long endTime = System.nanoTime();
	    long duration = endTime - startTime;
	    System.out.println(duration + "纳秒");
	}
	public static void traverseByFor(LinkedList<Integer> list)
	{
		long startTime = System.nanoTime();
		System.out.println("============for循环遍历=============="); 
	    for(Integer item : list)
	    {
	    	;
	    }
		long endTime = System.nanoTime();
	    long duration = endTime - startTime;
	    System.out.println(duration + "纳秒");
	}
}
输出:
5
============迭代器遍历==============
5900889纳秒
============随机索引值遍历==============
4745716889纳秒
============for循环遍历==============
5433777纳秒

Vector:

同步

import java.util.ArrayList;
import java.util.Iterator;
//Vector 几乎和ArrayList一样,除了Vector本身是同步的

public class ArrayListTest {
	public static void main(String[] a) {  
	    ArrayList<Integer> al = new ArrayList<Integer>();  
	    al.add(3);  
	    al.add(2);          
	    al.add(1);  
	    al.add(4);  
	    al.add(5);  
	    al.add(6);  
	    al.add(new Integer(6));  
	  
	    System.out.print("The third element is  ");
	    System.out.println(al.get(3));
	    al.remove(3);  //删除第四个元素,后面元素往前挪动
	    al.add(3, 9);  //将9插入到第4个元素,后面元素往后挪动
	    
	    System.out.println("======遍历方法=============");
	    
	    ArrayList<Integer> as = new ArrayList<Integer>(100000);
	    for (int i=0; i<100000; i++)
	    {
	    	as.add(i);
	    }
	    traverseByIterator(as);
	    traverseByIndex(as);
	    traverseByFor(as);    
	}  
	public static void traverseByIterator(ArrayList<Integer> al)
	{
		long startTime = System.nanoTime();
		System.out.println("============迭代器遍历=============="); 
	    Iterator<Integer> iter1 = al.iterator();  
	    while(iter1.hasNext()){  
	        iter1.next();  
	    }
		long endTime = System.nanoTime();
	    long duration = endTime - startTime;
	    System.out.println(duration + "纳秒");
	}
	public static void traverseByIndex(ArrayList<Integer> al)
	{
		long startTime = System.nanoTime();
		System.out.println("============随机索引值遍历=============="); 
	    for(int i=0;i<al.size();i++)
	    {
	    	al.get(i);
	    }
		long endTime = System.nanoTime();
	    long duration = endTime - startTime;
	    System.out.println(duration + "纳秒");
	}
	public static void traverseByFor(ArrayList<Integer> al)
	{
		long startTime = System.nanoTime();
		System.out.println("============for循环遍历=============="); 
	    for(Integer item : al)
	    {
	    	;
	    }
		long endTime = System.nanoTime();
	    long duration = endTime - startTime;
	    System.out.println(duration + "纳秒");
	}
}
输出:
The third element is  4
======遍历方法=============
============迭代器遍历==============
8145777纳秒
============随机索引值遍历==============
5244000纳秒
============for循环遍历==============
5088000纳秒


参考:中国大学mooc《java核心技术》

发布了55 篇原创文章 · 获赞 17 · 访问量 5013

猜你喜欢

转载自blog.csdn.net/weixin_43698704/article/details/103957837