2013-03-05 9 views
0

我需要重寫方法從索引移除項中減去索引所有其餘的項目號碼和1.重寫方法從索引移除項目由1

包括上述條件中減去索引的所有餘下項目號碼,我需要重寫「getLot」,因此它不依賴索引號來查找很多。它應該只能根據批號找到批次。因此,如果第2號批號被移除,那麼第3號批號將從第2號索引移至第1號索引,仍然會被找到第3批次,而不是第2號碼。 (指數從0開始的BTW)

這裏是我的代碼:

public Lot getLot(int lotNumber) 
{ 
    if((lotNumber >= 1) && (lotNumber < nextLotNumber)) { 
     // The number seems to be reasonable. 
     Lot selectedLot = lots.get(lotNumber - 1); 
     // Include a confidence check to be sure we have the 
     // right lot. 
     if(selectedLot.getNumber() != lotNumber) { 
      System.out.println("Internal error: Lot number " + 
           selectedLot.getNumber() + 
           " was returned instead of " + 
           lotNumber); 
      // Don't return an invalid lot. 
      selectedLot = null; 
     } 
     return selectedLot; 
    } 
    else { 
     System.out.println("Lot number: " + lotNumber + 
          " does not exist."); 
     return null; 
    } 
} 

謝謝。

+0

我重新標記的問題,因爲這似乎不是有什麼用JavaScript。如果有人能想到一個更好的標籤使用... – apnorton 2013-03-05 03:19:15

+0

爲什麼不使用鏈表 – Parth 2013-03-05 03:22:56

回答

1

可以使用commons lang'sArrayUtils爲:

array = ArrayUtils.removeElement(array, element)

+0

@machiavelli:如果你覺得它有用,那麼你可以把它標記爲接受 – Parth 2013-03-06 15:58:05

0

你有兩種方法

方法1:而不是使用陣列可以適應你的代碼使用ArrayList。

String[] lotArray; 
List<String> lotList = Arrays.asList(lotArray); 

lotList.remove("Lot 2"); 

方法2ArrayUtilsCommonLang

array = ArrayUtils.removeElement(array, element)