03-??.ppt - PowerPoint PPT Presentation

1 / 61
About This Presentation
Title:

03-??.ppt

Description:

Title: 03- .ppt Author: , , Last modified by: Created Date: 8/28/2000 6:52:12 AM Document presentation format – PowerPoint PPT presentation

Number of Views:110
Avg rating:3.0/5.0
Slides: 62
Provided by: 1857
Category:

less

Transcript and Presenter's Notes

Title: 03-??.ppt


1
?? ??
  • ?? ??(3? 27/28?)
  • 3? ??
  • ?? (3? 28/29?)
  • 3.5 ???
  • 3.6 ?? ??
  • ??
  • ??.

2
??
  • ?? (array)
  • ?? ???? ??? ??? ? ?? ??? ??? ???
  • ????? ?? ??
  • ?? ??? ??? ?? ????

3
3.1 Java? ??
  • ??? ???
  • ??? ??? t ??
  • t? ??? ?? ??
  • ? ?? ??? int?? ?? ??? int
  • ??? ?? ???
  • int a                // ??? ????? ?? int?
  • String args          // ??? ?? ?? String?
  • List lists            // ??? ????? ?? List?
  • double matrix     // ??? ?? ?? double ?
  • int x3d          // ??? ?? ?? int
    ?

4
  • new ???? ??? ??? ?? ??
  • a new int8   // 8?? int ??? ? ??? ??
  • d new Date()   // Date ??? ??
  • ????? ??? ??? new ???? ???? ? ???? ????? ? ??? 0?
    ??? ????
  • ??? ?? 0? ???? ???? ???
  • ??? ??? n? ? ???? ??? 0?? n-1? ?

5
  • ??? ??? length ??? ???? ??
  • int n a.length    // ?? a? ??? ?
  • Java? ???? ??? ???? ??? ??? ??? ???? ???? ??? ?
  • ??? ??? ???? ArrayIndexOutOfBoundsException ???
    ????
  • ??? ???? ??? ??? ???? ? ??
  • int a 44, 77, 22, 33, 66

6
  • ??? ??? ???? ???
  • ?? ??? ????? ??? ?? ?? ?? ?? ??? ?? ? ??.
  • int aa a
  • ?? ???? ???? ???? ???? ???? ???? ?? ????? ??? ?
    ??.
  • public void print(int a)
  • ??? ??? ???? ???? ????.
  • print(a)
  • ?? ??? ???? ?? ?? ??? ? ? ??.
  • public int randomArray(int n)
  • ? ??? ?? ??? ???? ??? ???? ?? ???. ?? ?? ??(?, ??
    ??)? ???? ??.
  • b a  // a? b? ?? ??? ?

7
  • ??? ????? System ???? ??? arraycopy() ???? ??? ?
    ??.
  • System.arraycopy(a, 0, b, 0, a.length)
  • ?? ??? ????? Object ???? ??? clone() ???? ??? ?
    ??.
  • b (int)a.clone()
  • clone()? ?? ?? ??? Object??? ??? ??? ????? ??.
  • ??? ?? for ??? ???? ????.
  • for (int i 0 i lt a.length i)
  • ai random.nextInt(1000)
  • ??? final? ???? ? ??? ???? ? ??.
  • final int a 22, 44, 66, 88
  • a3 99     // OK
  • a new int8    // ???

8
??? ???
  • LISTING 3.1 Testing Arrays
  • 1 public class Main
  • 2 private static java.util.Random random
    new java.util.Random()
  • 4 public static void main(String args)
  • 5 int a randomInts(10,1000)
  • 6 int aa (int)a.clone() // creates a
    duplicate of a in aa
  • 7 print(a)
  • 8 print(aa)
  • 9 a0 a1 a2 888
  • 10 print(a)
  • 11 print(aa)
  • 12
  • 14 public static int randomInts(int n, int
    range)
  • 15 int a new intn
  • 16 for (int i 0 i lt n i)
  • 17 ai random.nextInt(range)
  • 18 return a
  • 19
  • 21 public static void print(int a)

9
  • ?? ??
  •    102,955,717,249,649,596,131,414,849,759
  •    102,955,717,249,649,596,131,414,849,759
  •    888,888,888,249,649,596,131,414,849,759
  •    102,955,717,249,649,596,131,414,849,759

10
3.2 Java?? ??? ???
  • ??? ??
  • ??? ??? ???? ??? ?? ?? ??? ???
  • ? ??? ????? ??? ?? ??? ???
  • ? ??? ????? ??? ??? 16??? ???? ?
  • ?? ??? I_at_73d6a5
  • I ?? int? ???? ??
  • _at_73d6a5 ??? ??? ???? ??
  • ??? ?? ???
  • ???? ??? ??? ? ??? ???

11
?? ??? ???
  • LISTING 3.2 Printing an Array Reference
  • 1 public class Print
  • 2 public static void main(String args)
  • 3 int a 66, 33, 99, 88, 44, 55, 22
  • 4 System.out.println(a)
  • 5
  • 6
  • ?? ??
  •    I_at_73d6a5

12
3.3 ??? ?? ????
  • ??? ??? ??
  • ??? ????(??)?? ?? ??? ?? ?
  • ???? 3.1
  • ?? ????? ???? ???? .
  • ?? ??? ? m.
  • ??? ?? i? ??, amai.
  •    1. m0?? ??.
  •    2. ?? 3? i 1?? n-1?? ??.
  •    3. ?? aigtam??, m i? ??.
  •    4. m? ??.

13
  • ????(??)
  • ? ??? ??? ?? ??? ?
  • ????
  • ?? ????? ???? ???? ? ??? ? i? j.
  • ??? ?? ai? aj? ???
  •    1. x ai? ??.
  •    2. ai aj? ??.
  •    3. aj x? ??.
  • int ??? ?? swap ???
  • LISTING 3.3 The swap() Method
  • 1 void swap(int a, int i, int j)
  • 2 int ai ai, aj aj
  • 3 if (ai aj) return
  • 4 ai aj
  • 5 aj ai
  • 6

14
3.4 ??? ??
  • ??? ??
  • ????? ??
  • ?? ??? ??? ??
  • ?? ??
  • ?? ??? ?? ??? ?? ??? ??(???)? ??? ?? ??? ? ?? ??
    -gt ?? ??(heterogeneous array)? ??
  • ?? ??? ? ??? 3.4

15
??? ?? ??
  • LISTING 3.4 A Heterogeneous Array of Objects
  • 1 public class ObjectArray
  • 2 public static void main(String args)
  • 3 String s"Mercury"
  • 4 Float x new Float(3.14159)
  • 5 java.util.Date d new java.util.Date()
  • 6 int a new int 11, 33, 55, 77, 99
  • 7 Object objects s, x, d, a
  • 8 print(objects)
  • 9
  • 10

16
  • 11 static void print(Object a)
  • 12 System.out.print("" a0)
  • 13 for (int i1 ilta.length i)
  • 14 System.out.print("," ai)
  • 15 System.out.println("")
  • 16 if (a0 instanceof String)
  • 17 System.out.println(((String)a0).charAt
    (6))
  • 18 if (a1 instanceof Float)
  • 19 System.out.println(((Float)a1).isNaN())
  • 20 if (a2 instanceof java.util.Date)
  • 21 System.out.println(((java.util.Date)a
    2).getTime())
  • 22 if (a3 instanceof int)
  • 23 System.out.println(((int)a3).length)
  • 24
  • 25

17
  • ?? ??
  •      Mercury,3.14159,Sat May 12 124143 EDT
    2001,I_at_17d257
  •      y
  •      false
  •      989685703734
  •      5

18
3.5 ?? ??
  • ?? ?? (?? ?? ?? ?? ??)
  • ??? ?? ?? ?? ???? ????? ????? ??
  • ??? ??? ? ?? ??? ?? ??? ???? ??? ??? ??
  • ????
  • ?? ???? ? ?? ? x
  • ?? ??? ? i
  • ??? ai x ?? ?? aj?x? ? i -n
  •    1. 0?? n-1? ? i? ??, ?? 2? ??.
  •    2. ai x??, i? ??.
  •    3. -n? ??.
  • ?? ??? 3.5
  • ?? ??? T(n)

19
?? ??
  • LISTING 3.5 The Sequential Search
  • 1 public class TestSequentialSearch
  • 2
  • 3 public static void main(String args)
  • 4 int a 66, 44, 99, 33, 55, 22, 88, 77
  • 5 System.out.println("search(a," 55 ") "
    search(a,55))
  • 6 System.out.println("search(a," 50 ") "
    search(a,50))
  • 7
  • 9 public static int search(int a, int
    target)
  • 10 for (int i 0 i lt a.length i)
  • 11 if (ai target) return i
  • 12 return -a.length
  • 13
  • 14

20
  • ?? ??
  •      66,44,99,33,55,22,88,77
  •      search(a,55) 4
  •      search(a,50) -8

21
3.6 ??? ??
  • ??? ?? (asymptotically proportional)
  • ? ??? ??? ??? ? ??? ?? ?? ?? ????? ???? ????
    ????? ??
  • ? ? ?? f(n) 4n-3? g(n)n? ????? ??
  • ? ??? ??? f(n)/g(n) (4n-3)/(n) 4-3/n? 4? ??
    ???? ???
  • ? ?? g(n)/f(n) (n)/(4n-3) 1/(4-3/n)? 1? ??
    ???? ???
  • n? ??? 3/n? ?? ????? f(n)/g(n)? ?? 4? ????, ??
    f(n)4g(n)?? ??
  • g(n)n? ????? ???? ?? ??? ??? T(n)?? ??
  • f(n)4n-3? ??? ??? T(n)? ? ??? ?
  • ?? 4n-3?T(n) ?? 4n-3 T(n)?? ??.

22
  • ???? ?????? ?? ?? ??? T(1), T(lg n), T(n), T(n lg
    n), T(n2), T(n3), T(2n)? 7? ?? ??? ??? ??
  • ? ???? ??? ???(complexity class) ?? ??? ??
    ???(asymptotic growth class)?? ?
  • T(1) ?? ??(constant time)
  • T(n2) ?? ??(quadratic time)
  • T(n3) ?? ??(cubic time)
  • 5?? ??? ??? ???? ??
  • O(g(n)) f(n)f(n)/g(n)? ???? ???
  • O(g(n)) f(n)g(n)/f(n)? ???? ???
  • T(g(n)) f(n)f(n)/g(n)? g(n)/f(n)? ???? ???
  • ?(g(n)) f(n)n?8? ? f(n)/g(n)?0
  • ?(g(n)) f(n)n?8? ? g(n)/f(n)?0
  • big oh of g(n), big omega of g(n), big theta
    of g(n), little oh of g(n), little omega of
    g(n)??? ??

23
  • ??? ?????? ??
  • T(g) O(g)nO(g) 
  • ?(g(n)) ? O(g(n))?T(g(n))
  • ?(g(n)) ? O(g(n))?T(g(n)) 

24
3.7 ?? ??
  • ??? ????? ??? ?? ?? ?
  • ??? ????? ????? ??? ?????, ? ???? ? ??? ??? ????
    ??? ??
  • ????
  • ?? ????? ?? ? x
  • ?? ??? ? i
  • ??? ????? ???? ??
  • ??? ai x ?? ?? jltp? ??? ajltx?? ?? jp? ???
    ajgtx? ? i -p-1
  •    1. p0, qn-1? ??.
  •    2. pq?? ?? 2-5? ??.
  •    3. i (pq)/2? ??.
  •    4. ai x??, i? ??.
  •    5. ai lt x??, p i1? ?? ??? ??? q i-1? ??.
  •    6. -p-1? ??.
  • ?? ??? 3.6
  • ?? ??? T(lg n)

25
?? ??
  • LISTING 3.6 The Binary Search
  • 1 public class TestBinarySearch
  • 3 public static void main(String args)
  • 4 int a 22, 33, 44, 55, 66, 77, 88, 99
  • 5 System.out.println("search(a," 55 ")
    " search(a, 55))
  • 6 System.out.println("search(a," 50 ")
    " search(a, 50))
  • 7
  • 9 static int search(int a, int x)
  • 10 int p 0, q a.length-1
  • 11 while (p lt q) // search the segment
    ap..q
  • 12 int i (pq)/2 // index of element in the
    middle
  • 13 if (ai x) return i
  • 14 if (ai lt x) p i1 // search upper half
  • 15 else q i-1 // search lower half
  • 16
  • 17 return -p-1 // not found
  • 18
  • 19

26
  • ?? ??
  •      search(a,55) 3
  •      search(a,50) -4

27
3.8 java.util.Arrays ???
  • ?? ??? ?? ?? ???? ???? ??
  • ?? static ???? ?? ?? (??? Arrays? ?? ???)
  • ???? ??
  • public static int binarySearch(double a, double
    x)
  • public static boolean equals(double a, double
    b)
  • public static void fill(double a, double x)
  • public static void fill(double a, int lo, int
    hi, double x)
  • public static void sort(double a)

28
java.util.Arrays ???? ??
  • LISTING 3.7 Using the java.util.Arrays Class
  • 1 public class TestJavaUtilArrays
  • 3 public static void main(String args)
  • 4 int a 77, 44, 55, 22, 99, 66, 33, 88
  • 5 print(a)
  • 6 java.util.Arrays.sort(a)
  • 7 print(a)
  • 8 test(a,55)
  • 9 test(a,60)
  • 10 test(a,88)
  • 11 test(a,90)
  • 12
  • 13 static void test(int array, int target)
  • 14 int i java.util.Arrays.binarySearch(arra
    y,target)
  • 15 System.out.print("target"target", i"i)
  • 16 if (i gt 0) System.out.println("\ta" i
    "" arrayi)
  • 17 else System.out.println("\tInsert "
    target" at a"(-i-1)"")
  • 18
  • 20 static void print(int a)

29
  • ?? ??
  •    a 77,44,55,22,99,66,33,88
  •    a 22,33,44,55,66,77,88,99
  •    target55, i3  a355
  •    target60, i-5 Insert 60 at a4
  •    target88, i6  a688
  •    target90, i-8 Insert 90 at a7

30
3.9 ??? ?? IntArrays ???
  • int ??? ?? ??? ??(homegrown) IntArrays ??? ???
    3.8

31
  • LISTING 3.8 A User-De?ned Utility IntArrays
    Class
  • 1 public class IntArrays
  • 2 private static java.util.Random random
    new java.util.Random()
  • 4 / Determines whether the specified array is
    in ascending order.
  • 7 _at_param a the array.
  • 8 _at_return true if a is in ascending order,
    otherwise false. /
  • 10 public static boolean isSorted(int a)
  • 11 for (int i 1 i lt a.length i)
  • 12 if (ai lt ai-1) return false
  • 13 return true
  • 14
  • 16 / Prints all the elements in the
    specified array.
  • 19 _at_param a the array. /
  • 21 public static void print(int a)
  • 22 System.out.print("" a0)
  • 23 for (int i 1 i lt a.length i)
  • 24 System.out.print("," ai)
  • 25 System.out.println("")
  • 26

32
  • 28 / Returns a new array of n random integers.
    If rangegt0, then
  • 30 the elements will be uniformly distributed
    in the range
  • 31 0 lt ai lt range otherwise, they will
    range over all int values.
  • 34 _at_param n the length of the new array.
  • 35 _at_param range determines the range of the
    element values.
  • 36 _at_throws IllegalArgumentException.
  • 37 _at_return the new array.
  • /
  • 39 public static int randomInts(int n, int
    range)
  • 40 if (nlt0 rangelt2) throw new
    IllegalArgumentException()
  • 41 int a new intn
  • 42 for (int i0 iltn i)
  • 43 ai random.nextInt(range)
  • 44 return a
  • 45
  • 47 / Returns a new array of size n that
    contains the elements of
  • 49 the specified array a and 0s thereafter.
  • 51 _at_param a the array to be copied.
  • 52 _at_param n the length of the new array.

33
  • 56 public static int resize(int a, int n)
  • 57 if (nlta.length) throw new
    IllegalArgumentException()
  • 58 int aa new intn
  • 59 System.arraycopy(a, 0, aa, 0, a.length)
  • 60 return aa
  • 61
  • 63 / Interchanges element i with element j in
    the specified array.
  • 66 _at_param a the array.
  • 67 _at_param i index of one element.
  • 68 _at_param j index of the other element.
  • 69 /
  • 70 public static void swap(int a, int i,
    int j)
  • 71 int ai ai, aj aj
  • 72 if (ai aj) return
  • 73 ai aj
  • 74 aj ai
  • 75
  • 76

34
3.10 java.util.Vector ???  
  • ?? 1.1?? java.util ???? Vector ??? ??
  • ? ??? java.util.ArrayList ???? ??
  • ?????? ??(resizable array)
  • ? ??? ?? ???? ???? ? ? ??? ???? ????? ??
  • ? ??? ?? ??? 2?? ??? ?? ?
  • ?? ??? ?? ??
  • LISTING 3.9 Resizing an Integer Array
  • 1 int resized(int a)
  • 2 int na.length
  • 3 int aa new int2n
  • 4 System.arraycopy(a,0,aa,0,n)
  • 5 return aa
  • 6
  • Vector ???
  • ?? 3.4, ??? 3.10 3.13

35
Vector ???
36
java.util.Vector ???? ???? ??
  • LISTING 3.10 A Simpli?ed Version of the
    java.util.Vector Class
  • 1 public class Vector
  • 2 protected Object objects // backing
    array
  • 3 protected int size // actual number of
    Objects stored
  • 4 protected static final int CAPACITY10  
  • 6 public Vector(int capacity)  
  • 7 if (capacitylt0) throw new IllegalArgumentExce
    ption("capacitylt0")
  • 8 objects new Objectcapacity  
  • 9  
  • 11 public Vector()
  • 12 this(CAPACITY)
  • 13
  • 15 public int size()
  • 16 return size
  • 17
  • 19 //...
  • 21 protected void resize()
  • 22 int nobjects.length
  • 23 Object temp new Object2n

37
Vector ???? ?? toString() ???
  • LISTING 3.11 A toString() Method for the Vector
    Class
  • 1 public String toString()
  • 2 if (size 0) return "()"
  • 3 StringBuffer buf new StringBuffer("("
    objects0)
  • 4 for (int i 1 i lt size i)
  • 5 buf.append("," objectsi)
  • 6 return buf ")"
  • 7

38
??? ???? ???
  • LISTING 3.12 A Constructor That Loads an Array
  • 1 public Vector(Object a)
  • 2 int n a.length
  • 3 objects new Object2n
  • 4 System.arraycopy(a,0,objects,0,n)
  • 5 size n
  • 6

39
Vector ???? ???
  • LISTING 3.13 Testing the Vector Class
  • 1 public static void main(String args)
  • 2 Vector v new Vector(args)
  • 3 System.out.println(v)
  • 4
  • ?? ??
  • A\gtjava TestVector alpha beta gamma delta
  •      (alpha,beta,gamma,delta)

40
3.11 ??? ??
  • 2?? ?? ??? ??? ?? ??
  • 2?? ??? ?? ??? 3.14
  • ?? a
  • ??? ???? ??
  • ? ???? ??? ??? ?? -gt ????? ?? (ragged array)
  • ?? b
  • ??
  • ???? ?? 3? ??? 4-?? ??-??(?)

41
2-?? ??? ??
  • LISTING 3.14 Using Two-Dimensional Arrays
  • 1 public class TwoDimensionalArrays
  • 2 public static void main(String args)
  • 3 int a new int3 // an array of 3
    sub-arrays (rows)
  • 4 a0 new int22,44,66,88 // the first
    row
  • 5 a2 new int33,77 // the third row
  • 6 System.out.println("a " a "\na.length "
    a.length)
  • 7 IntArrays.print(a0)
  • 8 IntArrays.print(a2)
  • 9 System.out.println("a2.length "
    a2.length)
  • 10 int b 22,44,66,88 , // the
    first row of b
  • 11 0, 0, 0, 0 , // the second
    row of b
  • 12 33,55,77, 0 // the third row
    of b
  • 13 System.out.println("b " b "\nb.length
    " b.length)
  • 14 IntArrays.print(b0)
  • 15 IntArrays.print(b2)
  • 16 System.out.println("b2.length "
    b2.length)
  • 17
  • 18

42
  • ?? ??
  •    a I_at_cac268
  •    a.length 3
  •    22,44,66,88
  •    33,77
  •    a2.length 2
  •    b I_at_8d107f
  •    b.length 3
  •    22,44,66,88
  •    33,55,77,0
  •    b2.length 4

43
??? java.util.Vector ???
44
?? 3.2 ???? ???
  • ?? Pascals Triangle
  • ji? ? i? ? j? ??? ?? ?? cij? ??? 2-?? ??
  • ?? ?? cij ci-1,j-1ci-1,j ??
  • ?, ??? ??? ?? ?? ?? ?? ?? ??? ?? ?

45
???? ???
  • 1 public class PascalsTriangle
  • 2 public static void main(String args)
  • 3 int rows Integer.parseInt(args0)
  • 4 int a init(rows)
  • 5 print(a) 6
  • 8 static int init(int n)
  • 9 int a new intnn
  • 10 for (int i 0 i lt n i)
  • 11 for (int j 0 j lt i j)
  • 12 if (j 0 j i) aij 1
  • 13 else aij ai-1j-1
    ai-1j
  • 14 return a
  • 15
  • 17 static void print(int a)
  • 18 for (int i 0 i lt a.length i)
  • 19 for (int j 0 j lt i j)
  • 20 print(aij, 5)
  • 21 System.out.println() 22
  • 23

46
  • ??? ?? ?? 7(args0??)? ?? ?? ??
  •      1
  •      1   1
  •      1   2   1
  •      1   3   3   1
  •      1   4   6   4   1
  •      1   5  10  10   5   1
  •      1   6  15  20  15   6   1

47
3.12 ?? ?? ?? ??? ??
  • ????(concordance)
  • ??? ?? ?? ??? ?? ????? ? ??? ? ????? ?? ??(?, ?,
    ?, ???, ??, ?? ??)? ??
  • ?? ???? ??? ?? ??
  • ?? 3.3

48
?? 3.3 ??? ??? ? ?? ??
49
  • ?? ??? ?? ADT? ???? ??
  • ?(map)
  • ? ??? ?? ???? ???? ??-?? ?? ?? ??
  • ??? ??, ??? ? ??? ??? ? ?? ?? ??? ???
  • ?? x? ?(key), ?? y? ? ?(value)??? ?
  • ??, ?? ??, ?? ???(look-up table)???? ?

50
Map ??? ?? ?? ??? ??
  • ADT Map
  • ? Map? ??? ?????, ? ??? (?, ?) ???. ? ??? ????
    ????? ??.
  • String get(String key)
  •    ?? ??? ?? ?? ?? ?, ?? ???? ??? ?.
  •    ??? ? ?? ???? ??.
  • String put(String key, String value)
  •    ?? ??? ?? ?? ?? ?, ?? ???? ??? ?.
  •    ??? ?? ? (?, ?)? ?? ???.
  • int size()
  •    ?? ?? ?? ?? ?.
  •    ??? ? ?? ???? ??.
  • String toString()
  •    ?? ?? ???? ??? ??.
  •    ??? ? ?? ???? ??.

51
Map ?????
  • LISTING 3.16 A Map Interface
  • 1 public interface Map
  • 2 public String get(String key)
  • 3 public String put(String key, String
    value)
  • 4 public int size()
  • 5 public String toString()
  • 6

52
ArrayMap ???? ?? UML ?????
ArrayMap ???
53
?? ?? ?? ??
54
ArrayMap ???
  • LISTING 3.17 An ArrayMap Class
  • 1 public class ArrayMap implements Map
  • 2 private static final int INITIAL_LENGTH10
  • 3 private Entry a new EntryINITIAL_LENGTH
  • 4 private int size
  • 6 public String get(String key)
  • 7 for (int i 0 i lt size i)
  • 8 if (ai.key.equals(key)) return
    ai.value
  • 9 return null
  • 10
  • 12 public String put(String key, String
    value)
  • 13 for (int i 0 i lt size i)
  • 14 if (ai.key.equals(key)) return
    ai.setValue(value)
  • 15 if (size a.length) resize()
  • 16 asize new Entry(key,value)
  • 17 return null
  • 18
  • 20 private void resize()
  • 21 Entry aa new Entry2a.length

55
  • 26 public int size()
  • 27 return size
  • 28
  • 30 public String toString()
  • 31 StringBuffer buf new StringBuffer()
  • 32 for (int i 0 i lt size i)
  • 33 buf.append(ai "\n")
  • 34 return buf.toString()
  • 35
  • 37 private static class Entry
  • 38 String key, value
  • 40 public Entry(String key, String value)
  • 41 this.key key
  • 42 this.value value
  • 43
  • 45 public String setValue(String value)
  • 46 String oldValue this.value
  • 47 this.value value
  • 48 return oldValue

56
ArrayMap ???? ???
  • LISTING 3.18 Testing the ArrayMap Class
  • 1 import java.io.
  • // defines the FileReader and BufferedReader
    classes
  • 2 import java.util.StringTokenizer
  • 4 public class Main
  • 5
  • 6 public Main(String file)
  • 7 Map map new ArrayMap()
  • 8 int lineNumber 0
  • 9 try
  • 10 BufferedReader in new
    BufferedReader(new FileReader(file))
  • 11 String line in.readLine()
  • 12 while(line ! null)
  • 13 lineNumber
  • 14 StringTokenizer parser new
    StringTokenizer(line, " ,-.?!")
  • 15 while (parser.hasMoreTokens())
  • 16 String word parser.nextToken().toUp
    perCase()

57
  • 17 String list map.get(word)
  • 18 if (list null) map.put(word, ""
    lineNumber)
  • 19 else map.put(word, list ","
    lineNumber)
  • 20
  • 21 System.out.println(lineNumber "\t"
    line)
  • 22 line in.readLine()
  • 23
  • 24 in.close()
  • 25 catch(IOException e)
    System.out.println(e)
  • 26 System.out.println(map)
  • 27 System.out.println("lines "
    lineNumber)
  • 28 System.out.println("distinct words "
    map.size())
  • 29
  • 30
  • 31 public static void main(String args)
  • 32 new Main(args0)
  • 33
  • 34

58
  • ??? 3.18? ???? ?? ??? ? ?? ???? ???? ?????. ???,
    ???? ???? ??? ???? ?????.
  •    FRIENDS 1
  •    ROMANS 1
  •    COUNTRYMEN 1
  •    LEND 1
  •    ME 1,33,35
  •    YOUR 1
  •    EARS 1
  •    I 2,12,24,28,29,29,35
  •    COME 2,12,35
  •    TO 2,2,12,13,16,28,29,31,32,35
  •    BURY 2
  •    CAESAR 2,5,6,8,18,19,34
  •    ...

59
??? ??? ??? ??
  • LISTING 3.19 Inserting the Entries in
    Alphabetical Order
  • 1 public String put(String key, String value)
  • 2 int i0
  • 3 while (iltsize)
  • 4 if (ai.key.equals(key)) return
    ai.setValue(value)
  • 5 if (ai.key.compareTo(key) gt 0) break
  • 6 i
  • 7
  • 8 if (size a.length) resize()
  • 9 System.arraycopy(a, i, a, i1, size-i)
  • 10 ai new Entry(key, value)
  • 11 size
  • 12 return null
  • 13

60
?? ?? ?? ??
??? ? COFFIN? ?? ?? ??
61
?? ?? ?? ??
??? ? COFFIN? ??? ?? ?? ?? ?? ??
Write a Comment
User Comments (0)
About PowerShow.com