Array List n public Array Listint initial Capacity
Array. List
������� n public Array. List(int initial. Capacity) ¨ Throw Illegal. Argumant. Exception ��� initial. Capacity <0 Array. List<String>name = new Array. List<String>(100); n ����������� 10 ������ n
Shallow copy woman. List a w 1 w 2 w 3
Shallow copy (2( n ������� ¨ woman. List. add(new People(“MRS. X”)); ��� ¨ a. add(new People(“MR. A. J. ”)) ����� woman. List MRS. X a w 1 w 2 w 3 MR. A. J.
public E get(int index( �� ������ n Throw index. Out. Of. Bounds. Exception ��� size()<=index, ���� index<0 n
public E set(int index, E element) ��������� index ���� element n �� ����������� index ��� n Throw index. Out. Of. Bounds. Exception ��� size()<=index ���� index<0 n Worst time = constant n
public void add(int index, E element) ���� element ������ � ������� index n ����������� n Worst time = O(n( n Throw index. Out. Of. Bounds. Exception ��� size()< index ���� index<0 n
Array. List class heading n n public class Array. List<E> extends Abstract. List<E> implements List<E>, Random. Access, Cloneable, java. io. Serializable ������� ¨ private transient E[ ] element. Data; n Transient ������������� serialization ������� ¨ private int size;
�������� public Array. List (int initial. Capacity) { element. Data = new Object [initial. Capacity]; } public Array. List ( ) { this (10); }
public Array. List(Collection<? extends E> c) { this((c. size()*110)/100); // ������ 10% Iterator i = c. iterator( ); while (i. has. Next( )) element. Data[size++] = i. next(); }
��������� add(E element) public boolean add(E element){ ensure. Capacity(size+1); //���������� element. Data[size++] = element; return true; }
public void ensure. Capacity(int min) { modcount++; //������ int old. Capacity = element. Data. length; if(min>old. Capacity){ //��������� E old. Data[] = element. Data; int new. Capacity = (old. Capacity*3)/2+1; //������� 50% if(new. Capacity < min. Capacity) //������ new. Capacity = min; // ������������ element. Data = (E) new Object[new. Capacity]; System. array. Copy(old. Data, 0, element. Data, 0, size); }
mod. Count & expected. Mod. Count (��� ) ������� ����� public Mod. Count. Driver( ) { Array. List list = new Array. List( ); list. add (“yes”); ������������ Iterator itr = list. iterator( ); mod. Count++, ��� expected. Mod. Count ��� list. add (“good”); itr. next( ); // exception thrown at this point } next ��������
�������� public Object clone() { try { Array. List v = (Array. List)super. clone(); // copies size v. element. Data = new Object[size]; System. arraycopy(element. Data, 0, v. element. Data, 0, size); v. mod. Count = 0; return v; } catch (Clone. Not. Supported. Exception e) { // this shouldn't happen, since we are Cloneable throw new Internal. Error(); } }
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. public void process. Input (String s) { 1 int n = Integer. parse. Int (s); Array. List my. List = new Array. List (n); 2 for (int i = 0; i < n; i++) my. List. add (new Double (i)); 3 my. List. add (n / 2, new Double (7. 8)); my. List. remove (2 * n / 3); 4 double d = ((Double)my. List. get (n / 2)). double. Value( ) * 2. 5; my. List. set (n / 2, new Double (d)); 5 gui. println (my. List) ; } 6
����� Array. List – Very. Long. Int ������ Very. Long. Int n protected Array. List<Integer> digits; ¨ ������������
����� Array. List – Very. Long. Int class n public Very. Long. Int(String s) ¨ ����� Very. Long. Int ����� String ¨ Throw Null. Pointer. Exception ��� s == null Very. Long. Int a = new Very. Long. Int(“ 5629? 3”); n ���� 56293 ����� a n ¨ Worst time = O(string length) ������
public Very. Long. Int(String s){ final char LOWEST_DIGIT_CHAR = ‘ 0’; digits = new Array. List<Integer>(s. length()); char c; int digit; for(int i = 0; i<s. length(); i++){ c = s. char. At(i); if(Character. is. Digit(c)){ Avg time & digit = c - LOWEST_DIGIT_CHAR; worst time = O(n) digits. add(digit); // auto convert } Avg time = constant, } Worst time = constant �������� }
n public String to. String() ¨ Return String ������ Very. Long. Int object ��� n 56293 ����� [5, 6, 2, 9, 3] ¨ Worst time = O(������ ) public String to. String(){ return digits. to. String(); //������� }
n public void add(Very. Long. Int other. Very. Long) ¨ ������ Very. Long. Int ¨ Worst time = O(����������� ) ¨ Throw Null. Pointer. Exception ��� other. Very. Long == null
public void add(Very. Long. Int other. Very. Long){ final int BASE = 10; int larger. Size, partial. Sum, carry = 0; Array. List<Integer>sum. Digits= new Array. List<Integer>(); if(digits. size()>other. Very. Long. digits. size()) larger. Size = digits. size(); else ������� i ������ larger. Size = other. Very. Long. digits. size(); for(int i=0; i<larger. Size; i++){ partial. Sum = least(i) + other. Very. Long. least(i) + carry; O(n) carry = partial. Sum / BASE ; sum. Digits. add(partial. Sum % BASE); } Constant time if(carry==1) sum. Digits. add(carry); //��������� carry ���� Collections. reverse(sum. Digits); digits = sum. Digits; }
protected int least(int i){ if(i>=digits. size()) return 0; return digits. get(digits. size()-i-1); } Constant time
- Slides: 35