網(wǎng)上有很多關(guān)于pos機(jī)官網(wǎng)源碼,jdk源碼閱讀筆記的知識(shí),也有很多人為大家解答關(guān)于pos機(jī)官網(wǎng)源碼的問(wèn)題,今天pos機(jī)之家(www.nxzs9ef.cn)為大家整理了關(guān)于這方面的知識(shí),讓我們一起來(lái)看下吧!
本文目錄一覽:
pos機(jī)官網(wǎng)源碼
一、ArrayList概述
首先我們來(lái)說(shuō)一下ArrayList是什么?它解決了什么問(wèn)題?ArrayList其實(shí)是一個(gè)數(shù)組,但是有區(qū)別于一般的數(shù)組,它是一個(gè)可以動(dòng)態(tài)改變大小的動(dòng)態(tài)數(shù)組。ArrayList的關(guān)鍵特性也是這個(gè)動(dòng)態(tài)的特性了,ArrayList的設(shè)計(jì)初衷就是為了解決Java數(shù)組長(zhǎng)度不可變的問(wèn)題。我們都知道在Java中數(shù)組一旦被創(chuàng)建出來(lái),那么這個(gè)數(shù)組的大小就不可以改變了,而且初始化的時(shí)候就必須要指定數(shù)組的大小。在開(kāi)發(fā)的場(chǎng)景中很多時(shí)候我們并不知道我們的數(shù)據(jù)量有多少,如果數(shù)組創(chuàng)建得太大就會(huì)造成極大的空間資源的浪費(fèi),如果太小了又會(huì)報(bào)數(shù)組下標(biāo)越界異常。這是我們?cè)陂_(kāi)發(fā)的過(guò)程中使用數(shù)組來(lái)存儲(chǔ)數(shù)據(jù)時(shí)經(jīng)常會(huì)遇到的問(wèn)題,但是如果使用ArrayList的話,就可以輕易的解決這個(gè)問(wèn)題,它能夠根據(jù)你存放的數(shù)據(jù)的大小動(dòng)態(tài)的改變數(shù)組的大?。ū举|(zhì)創(chuàng)建新數(shù)組),所以我們?cè)趯?xiě)代碼的時(shí)候就不需要關(guān)心數(shù)組的大小了,我覺(jué)得這也是ArrayList創(chuàng)建出來(lái)所需要解決的問(wèn)題。當(dāng)然,如果在知道數(shù)組的大小且對(duì)數(shù)組的數(shù)據(jù)不增加的話,建議使用數(shù)組的存儲(chǔ)數(shù)據(jù),這樣對(duì)性能的提升有一定的幫助。
那么,ArrayList是怎么動(dòng)態(tài)擴(kuò)展數(shù)組大小的呢?下面通過(guò)源碼一步一步揭開(kāi)ArrayList的神秘面紗吧。
二、ArrayList全局變量
/** * Default initial capacity. */ //默認(rèn)的初始化大小 private static final int DEFAULT_CAPACITY = 10; /** * The array buffer into which the elements of the ArrayList are stored. * The capacity of the ArrayList is the length of this array buffer. Any * empty ArrayList with elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA * will be expanded to DEFAULT_CAPACITY when the first element is added. */ //這個(gè)是用來(lái)存放數(shù)據(jù)用的數(shù)組,add進(jìn)來(lái)的數(shù)據(jù)都是放到這個(gè)數(shù)組里面的 transient Object[] elementData; // non-private to simplify nested class access /** * The size of the ArrayList (the number of elements it contains). * * @serial */ //數(shù)組的大小 private int size;
三、ArrayList構(gòu)造函數(shù)
ArrayList的構(gòu)造函數(shù)有三個(gè):
1、ArrayList(int initialCapacity):initialCapacity,數(shù)組的初始化大小,該構(gòu)造器創(chuàng)建一個(gè)指定大小的空數(shù)組。
2、ArrayList():創(chuàng)建一個(gè)默認(rèn)大小為0的空數(shù)組
3、ArrayList(Collection<? extends E> c):創(chuàng)建一個(gè)與c一樣大小的數(shù)組,并將c的元素賦值到數(shù)組中。
** * Constructs an empty list with the specified initial capacity. * * @param initialCapacity the initial capacity of the list * @throws IllegalArgumentException if the specified initial capacity * is negative */ public ArrayList(int initialCapacity) {//創(chuàng)建一個(gè) initialCapacity大小的空數(shù)組 if (initialCapacity > 0) { this.elementData = new Object[initialCapacity]; } else if (initialCapacity == 0) { this.elementData = EMPTY_ELEMENTDATA; } else { throw new IllegalArgumentException("Illegal Capacity: "+ initialCapacity); } } /** * Constructs an empty list with an initial capacity of ten. */ public ArrayList() {//創(chuàng)建空數(shù)組 this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA; } /** * Constructs a list containing the elements of the specified * collection, in the order they are returned by the collection\'s * iterator. * * @param c the collection whose elements are to be placed into this list * @throws NullPointerException if the specified collection is null */ public ArrayList(Collection<? extends E> c) { //將c轉(zhuǎn)換成數(shù)組,toArray方法返回的數(shù)組類(lèi)型有可能不是Object類(lèi)型的 elementData = c.toArray(); if ((size = elementData.length) != 0) { // c.toArray might (incorrectly) not return Object[] (see 6260652) //數(shù)組類(lèi)型不是Object類(lèi)型,需要將類(lèi)型轉(zhuǎn)換成Object類(lèi),避免后面調(diào)用方法 // 的時(shí)候出現(xiàn)類(lèi)型轉(zhuǎn)換異常 if (elementData.getClass() != Object[].class) elementData = Arrays.copyOf(elementData, size, Object[].class); } else { // replace with empty array. this.elementData = EMPTY_ELEMENTDATA; } }
這里我們主要看一下Arrays.copyOf()方法是如何轉(zhuǎn)換類(lèi)型的:
public static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) { @SuppressWarnings("unchecked") T[] copy = ((Object)newType == (Object)Object[].class) ? (T[]) new Object[newLength] : (T[]) Array.newInstance(newType.getComponentType(), newLength); System.arraycopy(original, 0, copy, 0, Math.min(original.length, newLength)); return copy; }
這個(gè)方法中,如果傳入的類(lèi)型為Object類(lèi)型,那么就直接創(chuàng)建一個(gè)Object數(shù)組,否則創(chuàng)建一個(gè)對(duì)應(yīng)類(lèi)型的數(shù)組。然后調(diào)用System.arraycopy方法,將原數(shù)組賦值到新創(chuàng)建的數(shù)組中,強(qiáng)調(diào)一下,凡是使用到數(shù)組的地方就一定會(huì)使用到arraycopy的方法,這個(gè)方法我在String源碼閱讀有說(shuō)到過(guò),在這里我再跟大家說(shuō)一下這個(gè)方法吧。
public static native void arraycopy(Object src, int srcPos, Object dest, int destPos, int length);
這是一個(gè)本地方法所以無(wú)法繼續(xù)往下看源碼了,但是從源碼中可以知道每個(gè)參數(shù)代表的含義
src:原數(shù)組
srcPos:原數(shù)組中開(kāi)始復(fù)制的位置
dest:新數(shù)組,即目標(biāo)數(shù)組
destPos:目標(biāo)數(shù)組存放的位置,即從原數(shù)組的復(fù)制過(guò)來(lái)的元素從這個(gè)位置開(kāi)始放
length:復(fù)制數(shù)組的長(zhǎng)度
舉個(gè)代碼示例:
public static void main(String[] args) { int[] src = {1,2,3,4,5}; int[] dest = new int[6]; for (int i : dest) { System.out.print(i + " "); } System.out.println(); System.arraycopy(src,0,dest,0,src.length); for (int i : dest) { System.out.print(i + " "); } } //運(yùn)行結(jié)果: //0 0 0 0 0 0 //1 2 3 4 5 0
看示例代碼應(yīng)該能夠懂,第一次打印的時(shí)候dest只是被初始化沒(méi)有賦值,所以里面每個(gè)元素存放的都是默認(rèn)值,而int的默認(rèn)值為0,所以打印出來(lái)的都是0,之后arraycopy后將src的所有數(shù)據(jù)復(fù)制到dest從0位置開(kāi)始,所以打印的結(jié)果是 1 2 3 4 5 0
四、add方法
/** * Appends the specified element to the end of this list. * * @param e element to be appended to this list * @return <tt>true</tt> (as specified by {@link Collection#add}) */ public boolean add(E e) { //確認(rèn)當(dāng)前數(shù)組大小不會(huì)發(fā)生越界異常,否者對(duì)數(shù)組進(jìn)行擴(kuò)容 ensureCapacityInternal(size + 1); // Increments modCount!! elementData[size++] = e; return true; } /** * Inserts the specified element at the specified position in this * list. Shifts the element currently at that position (if any) and * any subsequent elements to the right (adds one to their indices). * * @param index index at which the specified element is to be inserted * @param element element to be inserted * @throws IndexOutOfBoundsException {@inheritDoc} */ public void add(int index, E element) { //檢查傳入的下標(biāo)是否在數(shù)組的范圍之內(nèi) rangeCheckForAdd(index); //檢查數(shù)組是否需要擴(kuò)容 ensureCapacityInternal(size + 1); // Increments modCount!! //在index位置的元素全部往后移一位,為添加進(jìn)來(lái)的元素騰出一個(gè)位置 System.arraycopy(elementData, index, elementData, index + 1, size - index); //將元素放入到index位置上 elementData[index] = element; size++; }
添加元素之前都需要檢查一下當(dāng)前的數(shù)組是否已經(jīng)滿(mǎn)了,如果滿(mǎn)了就按照添加元素后的大小進(jìn)行擴(kuò)容。可以說(shuō)在整個(gè)ArrayList類(lèi)中最核心的方法就是ensureCapacityInternal了,這個(gè)方法就是用來(lái)動(dòng)態(tài)擴(kuò)容的。
private void ensureCapacityInternal(int minCapacity) { ensureExplicitCapacity(calculateCapacity(elementData, minCapacity));}private void ensureExplicitCapacity(int minCapacity) { //修改次數(shù)+1,主要實(shí)現(xiàn)快速失敗機(jī)制的 modCount++; // overflow-conscious code //如果最小所需容量比當(dāng)前數(shù)組的長(zhǎng)度大的話就進(jìn)行擴(kuò)容 if (minCapacity - elementData.length > 0) grow(minCapacity); } /** * Increases the capacity to ensure that it can hold at least the * number of elements specified by the minimum capacity argument. * * @param minCapacity the desired minimum capacity */ private void grow(int minCapacity) { // overflow-conscious code int oldCapacity = elementData.length; //每次擴(kuò)容都是擴(kuò)大原來(lái)數(shù)組大小的1.5倍 int newCapacity = oldCapacity + (oldCapacity >> 1); if (newCapacity - minCapacity < 0) newCapacity = minCapacity; if (newCapacity - MAX_ARRAY_SIZE > 0) newCapacity = hugeCapacity(minCapacity); // minCapacity is usually close to size, so this is a win: //創(chuàng)建一個(gè)新的數(shù)組,長(zhǎng)度為 newCapacity,然后將原來(lái)數(shù)組的元素復(fù)制到新數(shù)組上并返回新數(shù)組,達(dá)到動(dòng)態(tài)擴(kuò)容數(shù)組的目的 elementData = Arrays.copyOf(elementData, newCapacity); }
在每次添加數(shù)據(jù)的時(shí)候都需要檢查一下添加數(shù)據(jù)后的長(zhǎng)度是否大于當(dāng)前數(shù)組的長(zhǎng)度,大于的話就創(chuàng)建一個(gè)大小為原來(lái)數(shù)組的1.5倍的新數(shù)組,然后將原數(shù)組的數(shù)據(jù)都復(fù)制到新數(shù)組中,最后將原數(shù)組的引用指向新數(shù)組,從而達(dá)到了擴(kuò)容的目標(biāo)。
五、get方法
/** * Returns the element at the specified position in this list. * * @param index index of the element to return * @return the element at the specified position in this list * @throws IndexOutOfBoundsException {@inheritDoc} */ public E get(int index) { rangeCheck(index); return elementData(index); }// Positional Access Operations @SuppressWarnings("unchecked") E elementData(int index) { return (E) elementData[index]; }
獲取數(shù)據(jù)的方法比較簡(jiǎn)單,直接根據(jù)數(shù)組的下標(biāo)index找到元素就行了,所以查找的速度比較快。
六、delete方法
/** * Removes the element at the specified position in this list. * Shifts any subsequent elements to the left (subtracts one from their * indices). * * @param index the index of the element to be removed * @return the element that was removed from the list * @throws IndexOutOfBoundsException {@inheritDoc} */ public E remove(int index) { rangeCheck(index); modCount++; E oldValue = elementData(index); //移動(dòng)區(qū)間 int numMoved = size - index - 1; if (numMoved > 0) //在刪除位置后面的所有元素都往前挪一位 System.arraycopy(elementData, index+1, elementData, index, numMoved); elementData[--size] = null; // clear to let GC do its work return oldValue; } /** * Removes the first occurrence of the specified element from this list, * if it is present. If the list does not contain the element, it is * unchanged. More formally, removes the element with the lowest index * <tt>i</tt> such that * <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt> * (if such an element exists). Returns <tt>true</tt> if this list * contained the specified element (or equivalently, if this list * changed as a result of the call). * * @param o element to be removed from this list, if present * @return <tt>true</tt> if this list contained the specified element */ public boolean remove(Object o) { if (o == null) { for (int index = 0; index < size; index++) if (elementData[index] == null) { fastRemove(index); return true; } } else { for (int index = 0; index < size; index++) if (o.equals(elementData[index])) { fastRemove(index); return true; } } return false; } /* * Private remove method that skips bounds checking and does not * return the value removed. */ private void fastRemove(int index) { modCount++; int numMoved = size - index - 1; if (numMoved > 0) System.arraycopy(elementData, index+1, elementData, index, numMoved); elementData[--size] = null; // clear to let GC do its work }
刪除有兩個(gè)重載的方法,一個(gè)是傳入一個(gè)數(shù)組的下標(biāo),另一個(gè)是傳入具體的內(nèi)容,但是最總還是根據(jù)equals方法查到index,然后根據(jù)index刪除元素。假設(shè)有個(gè)數(shù)組為 String[] strs = {"a","b","c","d","e"},現(xiàn)在調(diào)用 remove(3),那個(gè)大概流程為:
七、ArrayList的使用:
public static void main(String[] args) { List<integer> list = new ArrayList<>(); //添加操作 list.add(1); list.add(2); list.add(3); //刪除操作 list.remove(0); //查詢(xún)操作 //1、隨機(jī)訪問(wèn): for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i)); } //增強(qiáng)for循環(huán)遍歷 for (Integer integer : list) { System.out.println(integer); } //使用迭代器遍歷 Iterator<Integer> iterator = list.iterator(); while (iterator.hasNext()){ Integer integer = iterator.next(); System.out.println(integer); } }
八、總結(jié):
1、ArrayList的增刪改查等所有操作,其內(nèi)部都是對(duì)數(shù)組進(jìn)行操作。
2、ArrayList的動(dòng)態(tài)擴(kuò)容其本質(zhì)是創(chuàng)建一個(gè)更大的數(shù)組。
3、每次擴(kuò)容都擴(kuò)大到原來(lái)的1.5倍,1.5倍是比較理想的,如果每次擴(kuò)容太小的話就會(huì)頻繁擴(kuò)容,每次擴(kuò)容都需要進(jìn)行數(shù)組的復(fù)制,性能消耗比較大,應(yīng)該盡量避免。如果擴(kuò)容的倍數(shù)太大可能會(huì)造成空間的浪費(fèi)。
4、ArrayList允許存放null值。
九、建議:
1、在知道數(shù)據(jù)大小且后期不會(huì)在添加數(shù)據(jù)的情況下建議使用數(shù)組,而不是ArrayList;
2、初始化前估計(jì)數(shù)據(jù)量的大小,盡量指定ArrayList的初始化容量,避免進(jìn)行頻繁的數(shù)組復(fù)制操作;
3、在刪除比較多場(chǎng)景中不建議使用ArrayList;
4、在查多刪少的場(chǎng)景中建議使用ArrayList,原因是這個(gè)類(lèi)查找的速度非常快,時(shí)間復(fù)雜度為O(1),即不管數(shù)據(jù)量有多大,查找速度都一樣的,而且非???。但是刪除操作是比較慢的,上面我也有提到過(guò),ArrayList中每一次刪除一個(gè)數(shù)據(jù),后面所有的元素都要往前挪一位。如果數(shù)據(jù)量非常大,刪除的數(shù)據(jù)剛好在第一個(gè)位置,那個(gè)后面的所有元素都要前移,時(shí)間復(fù)雜度為O(N),這樣是非常耗費(fèi)時(shí)間的。
5、ArrayList是非線程安全的,如果在多線程環(huán)境中對(duì)安全的要求比較高的,建議使用 CopyOnWriteArrayList這個(gè)類(lèi),不懂得可以百度一下,或者將ArrayList轉(zhuǎn)成線程安全得,使用這種方式就可以:List list = Collections.synchronizedList(new ArrayList());
以上就是關(guān)于pos機(jī)官網(wǎng)源碼,jdk源碼閱讀筆記的知識(shí),后面我們會(huì)繼續(xù)為大家整理關(guān)于pos機(jī)官網(wǎng)源碼的知識(shí),希望能夠幫助到大家!









