Title: ArrayList
1ArrayList
2????? array ????????????
- ??? index ?????????????????????????????
- ????????????????????????????????
- ????????????????????????????????????????
- ?????????????????? List Interface
????????????????????????? - clone
- ensureCapacity
- trimToSize
- ????????? Object ????????
3?????????????
- public ArrayList(int initialCapacity)
- Throw IllegalArgumantException ???
initialCapacity lt0 - ArrayListltStringgtname new ArrayListltStringgt(100)
- ???????????????? ???????????????????????????? 10
??????
4???????????????????
- public ArrayList(Collectionlt? Extends Egt c)
- ????????? c ??????????????????????????????????????
- ???????????????????????????????????? c
- ????????????????????????????? 110 ??? c
- Worst time O(n)
- ???????????????? c ???????????????????????????????
???? ???????????????????????? - ???????? womanList ??????????????? Woman
?????????????????? People - ?????????????? ???????????????? People ??????
womanList - ArrayListltPeoplegt a new ArrayListltPeoplegt(womanL
ist)
5- ????????????????????????????????????????? c
???????? ????????????? shallow copy ???????? - ????????????????? clone()
- ArrayListltPeoplegt a (ArrayListltPeoplegt)womanList
.clone() - Shallow copy ?? reference ????????????????????
?????????????????????? ???????????????????????????
???????????????????????????? - ???????????????????? a womanList
???????????????? a ???????? womanList ???
6Shallow copy
womanList
w1
w3
w2
a
7Shallow copy (2)
- ?????????????? ??????????????
- womanList.add(new People(MRS.X)) ???
- a.add(new People(MR.A.J.)) ?????
8- new ArrayListltPeoplegt(womanList) ???
- (ArrayListltPeoplegt)womanList.clone()
- ????????????????????
- ?????????????????????????????????????
- ????????? copy constructor ???????????????? 110
???????????????
9????????????????????????????
- System.arraycopy(array1, i, array2, i2, 3)
- ?????????? array1 ??? index i
- ????? array2 ??????????? index i2
- ????????? 3 ???
10public boolean add(E element)
- ??????????????????????????
- Worst time O(n), Avg time constant
- ???????? true ?????????????????????? (????????
true ????)
11public E get(int index)
- ?????????????? ? ?????????????
- Throw indexOutOfBoundsException ???
size()ltindex, ???? indexlt0
12public E set(int index, E element)
- ?????????????????? index ???? element
- ???????????????????????????? index ???
- Throw indexOutOfBoundsException ??? size()ltindex
???? indexlt0 - Worst time constant
13public void add(int index, E element)
- ???? element ?????? ? ??????? index
- ????????????????????????????????
- Worst time O(n)
- Throw indexOutOfBoundsException ??? size()lt index
???? indexlt0
14public E remove(int index)
- ????????? ? ??????? index ?????
- ???????????????????????????????
- ??????????????????????????
- Throw indexOutOfBoundsException ??? size()ltindex
???? indexlt0
15public int indexOf(Object element)
- ????????????????? element ?????????????????????
- ???????? ?????????????????????????????? ???? -1
??????????? - ????????????????????????????????? equals()
- Worst time O(n)
16ArrayList class heading
- public class ArrayListltEgt extends
AbstractListltEgt implements ListltEgt, RandomAccess,
Cloneable, java.io.Serializable - ???????
- private transient E elementData
- Transient ?????????????????????????????
serialization ?????????????? - private int size
17??????????????????????????????
- public ArrayList (int initialCapacity)
- Â
- elementData new Object initialCapacity
- Â
-
- public ArrayList ( )
- Â
- this (10)
- Â
-
18- public ArrayList(Collectionlt? extends Egt c)
- Â
- this((c.size()110)/100) // ?????? 10
- Iterator i c.iterator( )
- while (i.hasNext( ))
- elementDatasize i.next()
- Â
19????????????????? ????? add(E element)
- public boolean add(E element)
- ensureCapacity(size1) //?????????????????????
- elementDatasize element
- return true
20public void ensureCapacity(int min)
- modcount //???????????
- int oldCapacity elementData.length
- if(mingtoldCapacity) //??????????????????????
- E oldData elementData
- int newCapacity (oldCapacity3)/21
//??????? 50 - if(newCapacity lt minCapacity) //???????????????
- newCapacity min // ???????????????????????
- elementData (E) new ObjectnewCapacity
- System.arrayCopy(oldData,0,elementData,0,size)
-
21modCount
- ????????????? ArrayList, LinkedList, TreeMap,
TreeSet, HashMap, HashSet - ??? ArrayList ???? inherit ????? AbstractList
- ??????????????????????????????? ???????? insert
???? remove ??? ArrayList ??? - ???????????????????????? expectedModCount ???????
- ??????????????????????????????????????????????????
?????? ???? itr.remove() - modCount ???????????????
- ???????????????????????????????
??????????????????????????????????????????????????
?????????????????????????????????????????????????
?????????????????????? - ????????????????????????????????????
??????????????? - if (modCount!expectedModCount)
- throw new ConcurrentModificationException()
Fail fast
22modCount expectedModCount (???)
??????????????????????????????????
- public ModCountDriver( )
- ArrayList list new ArrayList( )
- list.add (yes)
- Iterator itr list. iterator( )
- list.add (good)
- itr.next( ) // exception thrown at this point
?????????????????
modCount, ??? expectedModCount ????????
next ???????????????
23??????? ensureCapacity
- Worst time ?????????????????? O(n)
- Avg time
- ????????? add ?? n/3 ????????? ???????????????????
?????? - ?????? ????????? add ?? (n/3)1 ?????
??????????????????? n ?????? - ??????????????? ??????????? add
??????????????????????? n/((n/3)1) - ?????? 3 ?????
- Avg time constant
24????????????????
- public Object clone()
- try
- Â
- ArrayList v (ArrayList)super.clone()
// copies size - v.elementData new Objectsize
- System.arraycopy(elementData, 0,
v.elementData, - 0, size)
- v.modCount 0
- return v
-
- catch (CloneNotSupportedException e)
- // this shouldn't happen, since we are
Cloneable - throw new InternalError()
-
-
25????????????????? ????????????????????????????????
- ????? ArrayList ?????? n ??????
- ??? n ????? ????????????????? Double (i) ??? i
???????? 0 ????????????? - ??? new Double (7.8) ??? index n/2
- ??????????????????? 2n / 3 ??????
- ??? 2.5 ???????????????????????????
- ????????????????????????????????
26- public void processInput (String s)
- int n Integer.parseInt (s)
- ArrayList myList new ArrayList (n)
- for (int i 0 i lt n i)
- myList.add (new Double (i))
- myList.add (n / 2, new Double (7.8))
- myList.remove (2 n / 3)
- double d ((Double)myList.get (n /
2)).doubleValue( ) - 2.5
- myList.set (n / 2, new Double (d))
- gui.println (myList)Â
-
1
2
3
4
5
6
27????????? ArrayList VeryLongInt???????????
VeryLongInt
- protected ArrayListltIntegergt digits
- ???????????????????????????????????
28????????? ArrayList VeryLongInt class
- public VeryLongInt(String s)
- ????? VeryLongInt ????????? String
- Throw NullPointerException ??? s null
- VeryLongInt a new VeryLongInt(5629?3)
- ???????? 56293 ????????? a
- Worst time O(string length)
???????????????????
29??????? VeryLongInt(String s)
- ???????????????????????????
- ???????????????????????? ????????? 0
?????????????????????????? - ????????????????????????????
30- public VeryLongInt(String s)
- final char LOWEST_DIGIT_CHAR 0
- digits new ArrayListltIntegergt(s.length())
- char c
- int digit
- for(int i 0 ilts.length() i)
- c s.charAt(i)
- if(Character.isDigit(c))
- digit c - LOWEST_DIGIT_CHAR
- digits.add(digit) // auto convert
-
-
Avg time worst time O(n)
Avg time constant, Worst time constant
????????????????????????
31- public String toString()
- Return String ?????? VeryLongInt object ???
- 56293 ?????????? 5,6,2,9,3
- Worst time O(????????????)
- public String toString()
- return digits.toString()//??????????????????????
???????
32- public void add(VeryLongInt otherVeryLong)
- ?????? VeryLongInt
- Worst time O(???????????????????????????????????
??????) - Throw NullPointerException ??? otherVeryLong
null
33??????? add(VeryLongInt otherVeryLong)
- ??????????? ????????????????? (????????????????)
??????????????????????????????????????????????????
?????? sumDigits - ???????? 135 ??? 79 ???????????? sumDigits ????
412 ??????????????????????????? sumDigits
???????? ????????????????????????????????
sumDigits - ????? 214 ???????????????????
34- public void add(VeryLongInt otherVeryLong)
- final int BASE 10
- int largerSize, partialSum, carry 0
- ArrayListltIntegergtsumDigits new
ArrayListltIntegergt() - if(digits.size()gtotherVeryLong.digits.size())
- largerSize digits.size()
- else
- largerSize otherVeryLong.digits.size()
- for(int i0 iltlargerSize i)
- partialSum least(i) otherVeryLong.least(i)
carry - carry partialSum / BASE
- sumDigits.add(partialSum BASE)
-
- if(carry1)
- sumDigits.add(carry) //????????????????? carry
??????????? - Collections.reverse(sumDigits)
- digits sumDigits
-
??????? i ?????????????????
O(n)
Constant time
35- protected int least(int i)
- if(igtdigits.size())
- return 0
- return digits.get(digits.size()-i-1)
Constant time