2016-09-15 14 views
0

我試圖創建一個代碼來防止重複的數組元素。無論如何,我可以做到這一點,而無需創建一個arrayList?Java - 無論如何,我可以在不使用ArrayList的情況下防止重複數組?

當運行發生該錯誤的程序,當我進入第一#:在線程 「主」 java.lang.ArrayIndexOutOfBoundsException

例外:-1 在DuplicateElimination.main(DuplicateElimination.java:32)

這裏是我的代碼:

int[] numList = new int[5]; 
int newValue; 
boolean invalid; 

for (int i = 0; i < numList.length; i++){ 
    do{ 
     System.out.print("Please enter number"); 
     System.out.println(" "); 
     newValue = input.nextInt(); 

     //This is where the error occurs when I try to compare 
     //The last list element to the input value 
     invalid = numList[i-1] == newValue; 

     if(newValue < 10 || newValue > 100){ 
      System.out.print("Invalid number, Please enter a number between 10 and 100"); 
      newValue = input.nextInt(); 
     } 
     if(invalid){ 
      System.out.print("That number was entered already try again"); 
     } 
    }while(invalid); 

    insertIntoArray(numList, i, newValue); 
    printArray(numList); 
} 
+0

開始於:http://stackoverflow.com/questions/5554734/what-c​​auses-a-java-lang-arrayindexoutofboundsexception-and-how-do-i-prevent-it –

+1

可能的重複[我怎麼能測試一個數組是否包含某個值?](http://stackoverflow.com/questions/1128723/how-can-i-test-if-an-array-contains-a-certain-value) – Zircon

+2

你的'for'循環以'i = 0'開始,然後你試圖訪問'numList [i-1]',這有效地是'numList [-1]'。 -1不是一個合適的數組索引,並且錯誤消息很明顯。 –

回答

0

此答案需要您的問題標題的字,並防止重複,而不使用ArrayList或其他集合類。我改變了你的內心do環路:

 do { 
      System.out.print("Please enter number"); 
      System.out.println(" "); 
      newValue = input.nextInt(); 

      // See if value was already entered; search all array elements left of index i 
      int ix = 0; 
      while (ix < i && numList[ix] != newValue) { 
       ix++; 
      } 
      // now either ix == i or numList[ix] == newValue; 
      // if ix is not i, it means we encountered a duplicate left of index i 
      invalid = ix < i; 
      if (invalid) { 
       System.out.println("That number was entered already, try again"); 
      } else if (newValue < 10 || newValue > 100) { 
       System.out.println("Invalid number, please enter a number between 10 and 100"); 
       invalid = true; 
      } 
     } while (invalid); 

這可以確保最終插入numList[i]值滿足兩個條件:它在經過100爲10,這是不是重複。在所有先前輸入的值中搜索重複項(即,因爲沒有先前輸入的值,所以第一次沒有值)。

這不是我推薦用於生產的代碼(而是使用Set),但它對於練習來說很好。

1

您避免重複的藏品由

  1. 使用具有合理等於方法的類
  2. 然後使用;因爲這些,就其性質阻止你「收集」複製

另一種方法是:添加一個新的元素前;你只需迭代你的完整的現有數組,看看它是否已經包含了待添加的東西。如果是這樣,你的代碼會拒絕添加已知的「新」元素。

本質上:你絕對不需要「第二個」ArrayList來做到這一點。 如果您的應用程序的整個要點是「收集」某些對象,而沒有重複,那麼您只需使用Set。你只需將陣列踢出去;你只需使用一個Set。

0

還有一種使用「Arrays」類binarySearch方法的方法。

binarySearch方法接受要在數組上搜索的數組和鍵,並在找到鍵時返回索引。

數組輸入應該按排序順序。您可以使用Arrays.sort對數組進行排序並將其用作輸入。

實施例:

INT指數= Arrays.binarySearch(Arrays.sort(inputArray),密鑰);

如果找到了密鑰,請不要將該值添加到數組中。否則,將該值添加到數組。

0

你的代碼是好的只有這些發起矩陣出的位置,在這裏:

invalid = numList[i-1] == newValue; 

試試這個:

invalid = numList[i] == newValue; 
0

如果你想防止重複元素,HashSet是一個不錯的選擇。

在數組中,您需要一些東西來跟蹤重複的元素。 HashSet會爲你做O(1)時間複雜度。

使用HashSet.contains(Object),你可以檢查一個元素是否已經存在。使用HashSet.toArray(T[]),你可以獲得最後一個數組。

關於java.lang.ArrayIndexOutOfBounds,您的迭代從0開始可變,所以:

if i = 0; numList[i-1] = numList[-1] 

這是一個無效的索引,因爲數組的下標從0開始。因此,要麼改變是numList[i]或改變循環到for(int i = 1; i <= numList.length; i++)

+0

在這裏使用HashSet非常有用! –

相關問題