2010-11-15 108 views
2
class ArrayApp{ 

    public static void main(final String[] args){ 
     long[] arr; // reference to array 
     arr = new long[100]; // make array 
     int nElems = 0; // number of items 
     int j; // loop counter 
     long searchKey; // key of item to search for 
     // -------------------------------------------------------------- 
     arr[0] = 77; // insert 10 items 
     arr[1] = 99; 
     arr[2] = 44; 
     arr[3] = 55; 
     arr[4] = 22; 
     arr[5] = 88; 
     arr[6] = 11; 
     arr[7] = 00; 
     arr[8] = 66; 
     arr[9] = 33; 
     nElems = 10; // now 10 items in array 
     // -------------------------------------------------------------- 
     for(j = 0; j < nElems; j++){ 
      System.out.print(arr[j] + " "); 
     } 
     System.out.println(""); 
     // -------------------------------------------------------------- 
     searchKey = 66; // find item with key 66 
     for(j = 0; j < nElems; j++){ 
      if(arr[j] == searchKey){ 
       break; // yes, exit before end 
      } 
     } 
     if(j == nElems){ 
      System.out.println("Can’t find " + searchKey); // yes 
     } else{ 
      System.out.println("Found " + searchKey); // no 
     } 
     // -------------------------------------------------------------- 
     searchKey = 55; // delete item with key 55 
     for(j = 0; j < nElems; j++){ 
      if(arr[j] == searchKey){ 
       break; 
      } 
     } 
     for(int k = j; k < nElems - 1; k++){ 
      arr[k] = arr[k + 1]; 
     } 
     nElems--; // decrement size 
     // -------------------------------------------------------------- 
     for(j = 0; j < nElems; j++){ 
      System.out.print(arr[j] + " "); 
     } 
     System.out.println(""); 
    } // end main() 
} // end class ArrayApp 
  • 爲什麼我們用j和nElems搜索 數組。
  • 爲什麼我們再次將j分配給k爲 刪除?我們不能從j 本身刪除它嗎?
+0

是的神。一些簡單的格式化將使這一切更容易被吞下。 – skaffman 2010-11-15 13:16:27

+0

已經做到了,先生 – theband 2010-11-15 13:18:47

+1

不,我*已經做到了 – 2010-11-15 13:20:47

回答

0

nElemens用於加快搜索速度。在上面的例子中,數組有100個字段。所以你需要搜索所有的100個字段。但是因爲nElemen(元素數量)只有10個,所以只需要搜索10個元素而不是全部100.

但是要小心:上面的算法假定數組按照正確的順序填充,在有價值的領域之間沒有任何差距。

然後變量j被用作循環變量來訪問數組中的不同字段。

E.g.

arr [5]訪問數組的第6個字段。 arr [j]訪問j。數組中的元素。有關Java基本信息循環:

http://leepoint.net/notes-java/flow/loops/loops.html

0

這是從陣列中除去元素的困難(更不用說長)的方式。還有其他更簡單的方法,包括:

檢查這些在Java文檔。

+0

沒錯,但我認爲這裏要說明的是它的工作原理。 arraycopy和ArrayList.remove做基本上與上述相同的事情,雖然我認爲更有效。 – Jay 2010-11-15 14:18:40

0

實際上,在Java中從「[]」中刪除元素並不是一個好習慣。它們是靜態分配的,並且無論如何都不能改變[].length。您想做什麼?使用動態數組,列表等。例如,使用ArrayList

0

您的想法是在java數組(具有固定大小)上實現「可調整大小」數組。該固定陣列中的第一個nElem元素是您的邏輯陣列。現在,你希望能夠設置和刪除元素,並從這個邏輯陣列:

[00] [01] [02] [03] [04] [05] [06] [07] [08] [09] ... [99] // physical array 
[00] [01] [02] [03] [04] [05] [06] [07] [08]    // logical array, nElem = 9 
Insert '99' at (logical) position 04 
[00] [01] [02] [03] [99] [04] [05] [06] [07] [08]   // nElem = 10 
Delete value at (logical) position 03 
[00] [01] [02] [99] [04] [05] [06] [07] [08]    // nElem = 9 

的Java數組的大小仍然是100(它是固定的),邏輯數組的大小(nEleme)有每次插入和移除操作後都要進行調整。

如果插入元素,則必須將某些元素「向右移動」(並增加nElem)。你需要一個for循環和一個計數器(j)。如果你想刪除一個元素,你必須將元素「左移」到另一個元素,並且你需要一個計數器。

爲什麼我們用j和nElems來搜索一個數組。

要搜索的(未分類的)收集一個項目(陣列設置,..)你必須看看每個元素,也許從索引= 0至指數=(nelem個-1 )。如果當前位置的值與您的搜索條件匹配(例如:is-equal-to),則可以中斷搜索。 j存儲這個實際的指數,nElem(邏輯)陣列,(nElem-1)最後一個元素在此(邏輯),數組的索引)

爲什麼我們再次分配J確定K中缺失的大小?我們不能從j本身刪除它嗎?

僅供參考。這也可以工作,但很難理解。

for(j = 0; j < nElems; j++) { 
    if(arr[j] == searchKey) { 
     break; 
    } 
} 
for(;j < nElems-1; j++) { // note the missing initialzing value for the loop 
    arr[j] = arr[j+1]; 
} 
0

如果你有Apache的公地郎libary,你可以嘗試

inputArray = ArrayUtils.remove(inputArray , indexOfElement); 

該方法返回一個新的數組,通過從原始數組中找到的元素創建。