相比于书上的代码, 我觉得我改进的更清爽,耦合度更小啦。
输出都可以在方法里搞定了。。
1 /** 2 * 3 */ 4 5 /** 6 * @author home 7 * 8 */ 9 class HighArray 10 { 11 private long[] a; 12 private int nElems; 13 14 public HighArray(int max) 15 { 16 a = new long[max]; 17 nElems = 0; 18 } 19 public boolean find(long searchKey) 20 { 21 int j; 22 for(j = 0; j < nElems; j++) 23 if(a[j] == searchKey) 24 break; 25 if(j == nElems){ 26 System.out.println("Can't find searchKey : " + searchKey); 27 return false; 28 } 29 else{ 30 System.out.println("Found searchKey : " + searchKey); 31 return true; 32 } 33 } 34 public void insert(long value) 35 { 36 a[nElems] = value; 37 nElems++; 38 System.out.println("Insert value: " + value); 39 } 40 public boolean delete(long value) 41 { 42 int j; 43 for(j = 0; j < nElems; j++) 44 if(value == a[j]) 45 break; 46 if(j == nElems){ 47 System.out.println("Can't delete value: " + value); 48 return false; 49 } 50 else 51 { 52 for(int k = j; k < nElems; k++) 53 a[k] = a[k + 1]; 54 nElems--; 55 System.out.println("Delete value: " + value); 56 return true; 57 } 58 } 59 public void display() 60 { 61 for(int j = 0; j < nElems; j++) 62 System.out.print(a[j] + " "); 63 System.out.println(" "); 64 } 65 } 66 public class HighArrayApp { 67 68 /** 69 * @param args 70 */ 71 public static void main(String[] args) { 72 73 int maxSize = 100; 74 int searchKey; 75 HighArray arr; 76 arr = new HighArray(maxSize); 77 78 arr.insert(77); 79 arr.insert(99); 80 arr.insert(44); 81 arr.insert(55); 82 arr.insert(22); 83 arr.insert(88); 84 arr.insert(11); 85 arr.insert(00); 86 arr.insert(66); 87 arr.insert(33); 88 89 arr.display(); 90 91 searchKey = 35; 92 arr.find(searchKey); 93 94 searchKey = 88; 95 arr.find(searchKey); 96 97 arr.delete(00); 98 arr.delete(55); 99 arr.delete(99); 100 101 arr.display(); 102 103 arr.insert(687); 104 arr.insert(3234); 105 106 arr.display(); 107 } 108 109 }