배열 목록
기본 구현은 요소에 액세스하는 데 높은 효율을 갖는 배열입니다 (빠른 쿼리, 느린 삽입, 수정 및 요소 삭제)
LinkedList와 비교할 때 효율적이지만 스레드 안전합니다.
ArrayList 배열은 NULL을 포함한 모든 요소에 액세스 할 수있는 변이 가능한 배열입니다.
기본 구현은 배열을 사용하여 수행됩니다
과도 객체 [] elementData;
건축 방법
비공개 정적 최종 int default_capacity = 10; private static final object [] empty_elementData = {}; private static final object [] defaultCapacity_Empty_ElmentData = {}; 과도 객체 [] elementData; private int size; // 빈 목록 구성 public arraylist () {this.elementData = defaultCapacity_Empty_ElementData; } // 지정된 초기 용량 공개 ArrayList (int initialcapacity)의 빈 목록을 구성합니다. {if (initialCapacity> 0) {this.elementData = new Object [InitialCapacity]; } else if (initialCapacity == 0) {this.ElementData = empty_elementData; } else {새로운 불법 불법 행위 exception ( "불법 용량 :"+ 초기 범위); }} // 연결 요소의 반복 반환 순서대로 배열 된 지정된 수집 요소 목록을 구성합니다. 공개 arraylist (collection <? extends e> c) {elementData = c.toArray (); if (size = elementData.length)! = 0) {// c.toArray는 객체를 반환 할 수 없을 수 있습니다. } else {// 빈 배열로 교체합니다. this.elementData = empty_elementData; }}저장
// 목록의 지정된 위치에있는 요소는 요소로 대체되고 해당 위치의 원래 요소는 공개 E 세트 (int index, e element) {rangeCheck (index); // 배열 용량 및 던지기를 확인하십시오. indexOutOfBoundSexection e OldValue = ElementData (index); ElementData [index] = 요소; OldValue를 반환하십시오. } // 목록의 끝에서 지정된 요소를 추가 공개 부울 ADD (e e) {ensurecapacity internal (size + 1); // 배열 확장 ElementData [size ++] = e; 진실을 반환하십시오. } // 목록의 지정된 위치에서 요소를 공개 부울 add (e e) {enseerecapacity internal (size ++] = e; return true;} // 목록의 지정된 위치에서 요소 추가 공개 void add (int index, e element) {rangecheckforadd (index); size+1); // // sourmant * // sourmant! SRCPRO : 소스 배열 : 대상 배열 : 대상 배열의 시작 위치, 길이 : 현재이 위치에 요소를 이동시킵니다 목록의 끝에서의 요소는 연결 반복자 공개 부울에 의해 반환됩니다. 요소, numnew += numnew! = 0; ensurecacitacity (size + numnew).읽다
//이 목록의 지정된 위치에서 요소를 제거 공개 e 제거 (int index) {rangeCheck (index); 모드 카운트 ++; e OldValue = ElementData (색인); int nummoved = size -index -1; if (nummoved> 0) System.arrayCopy (ElementData, index+1, elementData, index, nummoved); ElementData [-size] = null; // GC가 작업을 수행하게하도록 지우기 위해 OldValue를 반환합니다. } //이 목록에서 요소를 제거 공개 부울 제거 (Object O) {if (o == null) {for (int index = 0; index <size; index+) if (elementData [index] == null) {fastremove (index); 진실을 반환하십시오. }} else {for (int index = 0; index <size; index ++) if (o.equals (elementData [index]) {fastremove (index); 진실을 반환하십시오. }} 거짓을 반환합니다. } 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; // GC가 작업을 수행하도록 지우기}배열 확장
요소가 배열에 추가 될 때마다 요소 수가 요소를 추가 한 후 현재 배열의 길이를 초과하는지 확인해야합니다. 길이를 초과하면 데이터 추가의 요구를 충족시키기 위해 배열이 확장됩니다.
public void ensurecapacity (int mincapacity) {int minexpand = (elementData! = defaultCapacity_Empty_ElementData)? 0 : default_capacity; if (mincapacity> minexpand) {restexplicitCapacity (mincapacity); }} private void enseUrecApacityInternal (int mincapacity) {if (elementData == defaultCapacity_Empty_ElementData) {minCapacity = math.max (default_capacity, minCapacity); } restexplicitCapicacity (mincapacity); } private void inricexplicitCapicacity (int mincapacity) {modcount ++; // 오버 플로우에 민감한 코드 if (minCapacity -ElementData.length> 0) grow (mincapacity); } private void grow (int mincapacity) {// 오버 플로우에 민감한 코드 int oldcapacity = elementData.length; int newCapacity = OldCapacity + (OldCapacity >> 1); if (newCapacity -MinCapacity <0) newCapacity = mincapacity; if (newCapacity -max_array_size> 0) newCapacity = hugecapacity (mincapacity); // mincapacity는 일반적으로 크기에 가깝기 때문에 이것은 다음과 같습니다. elementData = arrays.copyof (elementData, newCapacity); } private static int hugecapacity (int mincapacity) {if (mincapacity <0) // 오버 플로우 새로운 outofMemoryError (); return (mincapacity> max_array_size)? integer.max_value : max_array_size; }필기 배열 목록
공개 클래스 myArrayList /*구현 목록 <E>* /{개인 과도 객체 [] elementData; 개인 INT 크기; // 요소 수 공개 myArrayList () {this (10); } public myArrayList (int initialcapication) {if (initialCapacity <0) {try {새 예외 (); } catch (예외 e) {e.printstacktrace (); }} elementData = 새 개체 [초기 커피 션]; } public int size () {반환 크기; } public boolean isempty () {return size == 0; } // 인덱스에 따라 객체를 삭제합니다. 공개 void 제거 (int index)는 예외 {rangecheck (index); int nummoved = size-index-1; if (nummoved> 0) {System.ArrayCopy (ElementData, index+1, elementData, index, nummoved); } elementData [-size] = null; } // 객체 삭제 공개 부울 제거 (오브젝트 obj)는 예외를 {for (int i = 0; i <size; i ++) {if (get (i) .equals (obj)) {remove (i); } true를 반환합니다. } true를 반환합니다. } // 요소 수정 공개 객체 세트 (int index, object obj)는 예외 {rangecheck (index); Object OldValue = ElementData [index]; ElementData [index] = obj; OldValue를 반환하십시오. } // 지정된 위치에 요소를 삽입 공개 void add (int index, object obj) 예외 {rangecheck (index); ensurecapacity (); System.ArrayCopy (ElementData, Index, ElementData, Index+1, Size-Index); ElementData [index] = obj; 크기 ++; } public void add (객체 개체) {enseRecApacity (); /*elementData [size] = 객체; size ++;*/ elementData [size ++] = 객체; // 값을 먼저 할당 한 다음 자신을 증가시킵니다} 공개 객체 get (int index) 예외 {rangecheck (index); return ElementData [index]; } public void rangecheck (int index)는 예외를 던져 {if (index <0 || index> = size) {throw new Exception (); }} // public void ensurecapacity () {// 배열 확장 및 컨텐츠 사본 if (size == elementData.length) {// elementData = new Object [size*2+1]; 원래 배열의 내용을 이런 방식으로 작성하십시오. 객체 [] newArray = new Object [size*2+1]; // (int i = 0; i <newArray.length; i ++) {newArray [i] = ElementData [i]; }*/ system.arraycopy (ElementData, 0, NewArray, 0, elementData.length); elementData = newArray; }} // test public static void main (String [] args) {myArrayList myArrayList = new MyArrayList (3); myArrayList.add ( "111"); myArrayList.add ( "222"); myArrayList.add ( "333"); myArrayList.add ( "444"); myArrayList.add ( "555"); {myArrayList.remove (2); myArrayList.add (3, "새 값"); myArrayList.set (1, "modify"); } catch (예외 e1) {// todo 자동 생성 캐치 블록 e1.printstacktrace (); } system.out.println (myArrayList.size ()); for (int i = 0; i <myarraylist.size (); i ++) {try {system.out.println (myArrayList.get (i)); } catch (예외 e) {e.printstacktrace (); }}}}