2016-03-06 233 views
0

我需要一些幫助,夥計們。 這是分配:創建一個程序,以從隨機數組中刪除所有重複項。例如,如果陣列具有的值 4,7,10,4,9,5,10,7,3,5 則數組應改爲 4,7,10,9,5,3從陣列中刪除重複項

程序應該由兩個類DeleteDuplicate和DeleteDuplicateDemo組成。

DeleteDuplicate類應執行以下操作 1.有一個方法來檢查重複值。陣列 3. currentsize的 2.跟蹤有刪除重複的方法(與數組中的下一個值代替重複的值。)

的DeleteDuplicateDemo類應做到以下幾點 1.讓你的主要方法。 2.創建一個長度爲10到10的數字的隨機數組。 3.顯示原始隨機數組。 4.調用DeleteDuplicate方法來查找和刪除重複項。 5.顯示沒有重複的新陣列。

樣本輸出: [8,5,7,3,2,5,6,3,6,7]

[8,5,7,3,2,6]

下面是DeleteDuplicateDemo代碼:

import java.util.*; 

public class DeleteDuplicateDemo 
{ 
    public static void main(String[] args) 
    { 

     Random random = new Random(); 
     int array[]= new int[10]; 
     for (int i = 0; i < array.length; i++) 
     { 
      array[i] = random.nextInt(10)+ 1; 
     } 
     for (int i = 0; i < array.length; i++) 
     {   
      System.out.print(array[i] + " "); 
     } 
    System.out.println(); 
    DeleteDuplicate class1 = new DeleteDuplicate(); 
    array = class1.removeDuplicates(array); 

for (int i = 0; i < array.length; i++) 
     {   
      System.out.print(array[i] + " "); 
     } 

    } 
} 

這裏是DeleteDuplicate代碼:

import java.util.Arrays; 

public class DeleteDuplicate 
{ 
    private static int[] remove(int[] array) 
    { 
     int current = array[0]; 
     boolean found = false; 

     for (int i = 0; i < array.length; i++) 
     { 
     if (current == array[i] && !found) 
     { 
      found = true; 
     } 
     else if (current != array[i]) 
     { 
      System.out.print(" " + current); 
      current = array[i]; 
      found = false; 
     } 
    } 
     System.out.print(" " + current); 
     return array; 
    } 

    public static int[] removeDuplicates(int[] array) { 
     // Sorting array to bring duplicates together  
     Arrays.sort(array); 

     int[] finalArray = new int[array.length]; 
     int previous = array[0]; 
     finalArray[0] = previous; 

     for (int i = 1; i < array.length; i++){ 
     int value = array[i]; 

     if (previous != value){ 
      finalArray[i] = value; 
     } 
     previous = value; 
     } 
     return finalArray; 

     } 
} 

我怎樣才能使它發揮作用?我只有在打電話給另一個班時遇到問題。謝謝!

+0

'我只有與調用另一個class.'一個問題,是什麼問題?我看到的明顯問題是'removeDuplicates'是靜態的,所以你不需要'DeleteDuplicate'實例來調用它。另外,如果你發現一個重複的'i'仍然增加,所以你的'finalArray'中有「漏洞」 - 是否有意? – John3136

+0

remove()應該做什麼?我看到你使用布爾查找找到重複,但你實際上並沒有將它用於循環之外的任何其他地方。 –

+0

remove()應該檢查重複項並跟蹤當前大小。問題是我不確定如何從另一個類DeleteDuplicate調用方法。下面的評論員對此有所幫助,但現在代碼刪除了最小值,並將0代入刪除的值。 –

回答

0

在你DeleteDuplicateDemo類,改變

DeleteDuplicate class1 = new DeleteDuplicate(); 
array = class1.removeDuplicates(array); 

array = DeleteDuplicate.removeDuplicates(array); 

在你DeleteDuplicate類,改變

for (int i = 1; i < array.length; i++){ 
    int value = array[i]; 
    if (previous != value){ 
     finalArray[i] = value; 
    } 
    previous = value; 
} 

for (int i = 1, j = 0; i < array.length; i++){ 
    int value = array[i]; 
    if (previous != value){ 
     finalArray[j] = value; 
     j++; 
    } 
    previous = value; 
} 

然後,當您第二次向控制檯打印出array的值時,請務必在打印前檢查array[i] != nullarray[i]

0

刪除代碼過於複雜:如果您寫入的數據與寫入的數據不同,則不必通過從索引1開始的數組遍歷數組,而是單獨創建一個刪除項目的方法在索引i-1項目:

int rdIndex = 1; 
int wrIndex = 1; 
while (rdIndex != array.length) { 
    if (array[wrIndex-1] != array[rdIndex]) { 
     array[wrIndex++] = array[rdIndex]; 
    } 
    rdIndex++; 
} 
return Arrays.copyOf(array, wrIndex); 

在循環wrIndex的端部指示要被複制到結果數組的項數。

Demo.

+0

非常感謝。我希望我可以使用wrIndex,但我需要以一種複雜的方式來完成。所以數組打印出來,然而,它刪除了數組中最小的值,並將0代入刪除的數字中。 –

0

試試這個。

public static int[] removeDuplicates(int[] array) { 
    return IntStream.of(array).distinct().toArray(); 
} 

測試:

int[] array = {8, 5, 7, 3, 2, 5, 6, 3, 6, 7}; 
System.out.println(Arrays.toString(removeDuplicates(array))); 
// -> [8, 5, 7, 3, 2, 6]