JAVA學習筆記總結(六):用面向對象的方法實現數組的插入,刪除,查找,顯示

第一個例子:我們把數據存儲結構,也就是數組從程序的其他部分中分離出來,程序的其他部分將成爲這個結構的用戶。我們把程序分成兩個class,:

    一個class用來封裝數組(LowArray),外界不能直接訪問這個數組,但是該封裝提供了供外界訪問的方法setElem()和getElem(),該方法是外界應用和該數組之間通信的方式。

    另一個class創建了一個LowArray的對象,用這個對象來操作數組中的數據。

container class - 像LowArray這樣用來封裝數據對象的類稱爲容器類。一般容器類不僅提供數據的存儲,還提供訪問該數據的方法,也許還會封裝更復雜的操作。

class interface - 類的接口是用戶訪問該類的方法。由於class fields一般都是private的,所以我們說的接口一般指的都是類的方法。

abstrction - The process of seperating the how from the what - how an operation is performed inside a class, as oppose to what's visible to the class user.

第二個例子:我們進一步改進數組這個數據存儲結構使其更好的面向對象(HighArray)。class用戶不用再考慮數組的index, 數組的封裝不再使用getElem()和setElem()這樣的方法,取而代之的是insert(), find() 和delete(). 這使得class用戶可以把注意力放在what而不是how, 也就是說用戶只用考慮要插入/刪除/查找什麼,而不用擔心怎麼去插入/刪除/查找。

class LowArray {
 private long[] a;
 
 // constructor
 public LowArray(int size) {
  a = new long[size];
 }
 // set value
     public void setElem(int index, long value) {
      a[index] = value;
     }
     //get value
     public long getElem(int index) {
      return a[index];
     }
    
}  // end class LowArray


class LowArrayApp {
 public static void main(String[] args) {
  LowArray arr;
  arr = new LowArray(100);
  int nElems = 0;
  int j;
  
  // insert 10 items
  arr.setElem(0, 77);
  arr.setElem(1, 99);
  arr.setElem(2, 44);
  arr.setElem(3, 55);
  arr.setElem(4, 22);
  arr.setElem(5, 88);
  arr.setElem(6, 11);
  arr.setElem(7, 00);
  arr.setElem(8, 66);
  arr.setElem(9, 33);
  nElems = 10;
  
  // display items
  for(j=0; j<nElems;j++) {
   System.out.print(arr.getElem(j) + " ");
  }
  System.out.println("");
  
  // search for data item 26
  int searchKey=26;
  for(j=0; j<nElems; j++) {
   if(arr.getElem(j) == searchKey) {
    break;
   }
  }
  if(j == nElems) {
   System.out.println("Cannot find " + searchKey);
  }
  else {
   System.out.println("Found " + searchKey);
  }
  
  // delete value 55
  for(j=0; j<nElems;j++) {
   if(arr.getElem(j) == 55) {
    break;
   }
  }
  for(int k=j; k<nElems-1; k++) {
   arr.setElem(k, arr.getElem(k+1));
  }
  nElems--;
  
  // display items
  for(j=0; j<nElems; j++) {
   System.out.print(arr.getElem(j) + " ");
  }
  System.out.println("");
 } // end main()
}  // end class LowArrayApp

 

==========================================================================================================================

class HighArray {
 private long[] a;
 private int nElems;
 
 // constructor
 public HighArray(int max) {
  a = new long[max];
  nElems = 0;
 }
 
 // find specified value
 public boolean find(long searchKey) {
  int j;
  for(j=0; j<nElems; j++) {
   if(a[j] == searchKey) {
    break;
   }
  }
     if(j == nElems) {
      return false;
     }
     else {
      return true;
     }
 }  // end find()
 
 // put element into array
 public void insert(long value) {
  a[nElems] = value;
  nElems++;
 }
 
 // delete specified value
 public boolean delete(long value) {
  int j;
  for(j=0; j<nElems; j++) {    // look for it
   if(value == a[j]) {
    break;
   }
  }
  if(j == nElems) {
   return false;            // can't find it
  }
  else {
   for(int k=j; k<nElems; k++) {
    a[k] = a[k+1];         // move higher ones down
   }
   nElems--;
   return true;
  }
 }  // end delete()
 
 // display items
 public void display() {
  for(int j=0; j<nElems; j++) {
   System.out.print(a[j] + " ");
  }
  System.out.println("");
 } // end display()

} // end class HighArray

class HighArrayApp {
 public static void main(String[] args) {
  int maxSize = 100;
  HighArray arr;
  arr = new HighArray(maxSize);
  
  // insert 10 items
  arr.insert(77);
  arr.insert(99);
  arr.insert(44);
  arr.insert(55);
  arr.insert(22);
  arr.insert(88);
  arr.insert(11);
  arr.insert(00);
  arr.insert(66);
  arr.insert(33);
  
  // display items
  arr.display();
  
  // search for item 35
  int searchKey = 35;
  if(arr.find(searchKey)) {
   System.out.println("Found " + searchKey);
  }
  else {
   System.out.println("Can't find " + searchKey);
  }
  
  // delete 3 items
  arr.delete(00);
  arr.delete(55);
  arr.delete(99);
  
  // display items
  arr.display();
  
 }  // end main()
} // end class HighArrayApp

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章