ArrayList - PowerPoint PPT Presentation

About This Presentation
Title:

ArrayList

Description:

????????????????????????????????. ???????????????????????????????????????? ... public E set(int index, E element) ?????????????????? index ???? element ... – PowerPoint PPT presentation

Number of Views:413
Avg rating:3.0/5.0
Slides: 36
Provided by: Toe
Category:
Tags: arraylist | eset

less

Transcript and Presenter's Notes

Title: ArrayList


1
ArrayList
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 ???

6
Shallow copy
womanList
w1
w3
w2
a
7
Shallow 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 ???

10
public boolean add(E element)
  • ??????????????????????????
  • Worst time O(n), Avg time constant
  • ???????? true ?????????????????????? (????????
    true ????)

11
public E get(int index)
  • ?????????????? ? ?????????????
  • Throw indexOutOfBoundsException ???
    size()ltindex, ???? indexlt0

12
public E set(int index, E element)
  • ?????????????????? index ???? element
  • ???????????????????????????? index ???
  • Throw indexOutOfBoundsException ??? size()ltindex
    ???? indexlt0
  • Worst time constant

13
public void add(int index, E element)
  • ???? element ?????? ? ??????? index
  • ????????????????????????????????
  • Worst time O(n)
  • Throw indexOutOfBoundsException ??? size()lt index
    ???? indexlt0

14
public E remove(int index)
  • ????????? ? ??????? index ?????
  • ???????????????????????????????
  • ??????????????????????????
  • Throw indexOutOfBoundsException ??? size()ltindex
    ???? indexlt0

15
public int indexOf(Object element)
  • ????????????????? element ?????????????????????
  • ???????? ?????????????????????????????? ???? -1
    ???????????
  • ????????????????????????????????? equals()
  • Worst time O(n)

16
ArrayList 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

20
public 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)

21
modCount
  • ????????????? ArrayList, LinkedList, TreeMap,
    TreeSet, HashMap, HashSet
  • ??? ArrayList ???? inherit ????? AbstractList
  • ??????????????????????????????? ???????? insert
    ???? remove ??? ArrayList ???
  • ???????????????????????? expectedModCount ???????
  • ??????????????????????????????????????????????????
    ?????? ???? itr.remove()
  • modCount ???????????????
  • ???????????????????????????????
    ??????????????????????????????????????????????????
    ?????????????????????????????????????????????????
    ??????????????????????
  • ????????????????????????????????????
    ???????????????
  • if (modCount!expectedModCount)
  • throw new ConcurrentModificationException()

Fail fast
22
modCount 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
  1. public void processInput (String s)
  2. int n Integer.parseInt (s)
  3. ArrayList myList new ArrayList (n)
  4. for (int i 0 i lt n i)
  5. myList.add (new Double (i))
  6. myList.add (n / 2, new Double (7.8))
  7. myList.remove (2 n / 3)
  8. double d ((Double)myList.get (n /
    2)).doubleValue( )
  9. 2.5
  10. myList.set (n / 2, new Double (d))
  11. 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
Write a Comment
User Comments (0)
About PowerShow.com