2014-08-29 43 views
-1

需求是檢查數組中的重複項並刪除它,數組中的其餘項必須移到左邊。Java數組和循環導致錯誤輸出

我寫了這個:

public class TestRepeat { 

public static int deleteRepeats(char[] ch) { 

    int count = 0; 
    for (int x = 0; x < ch.length; x++) { 
     for (int y = x + 1; y < ch.length; y++) { 
      if (ch[x] == ch[y]) { 
       for (int k = y; k < ch.length - 1; k++) {//shifts the array to the right if its a duplicate 
        ch[k] = ch[k + 1]; 
       } 
       ch[ch.length - 1] = ' ';//replaces the last array with a blank 
      } 
     } 
    } 
    for (int q = 0; q < ch.length; q++) { 
     if (ch[q] == ' ') { 
      count++; 
     } 
    } 
    return count;//returns the number of deleted items 
} 

public static void main(String[] args) { 
    char[] ch = {'k', 'a', 'm', 'o', 'k', 'm', 'y', 'm', 'k', 'k', 'x', 'm', 'm', 'o'}; 
    System.out.print("The original array is: "); 
    System.out.println(ch); 
    System.out.println("Number of deleted characters: " + deleteRepeats(ch)); 
    System.out.print("The new array is: "); 
    System.out.println(ch); 
} 

} 

它應該返回:

原始數組:kamokmymkkxmmo

刪除字符數:8

新的陣列:kamoyx

而是將返回:

原始數組:kamokmymkkxmmo

刪除字符數:6

新數組是:kamoykxm

是什麼原因造成的問題,我該如何解決它?

+0

這可能幫助:http://stackoverflow.com/q/17967114/59087 – 2014-08-29 04:45:23

+0

計算器是不正確的門戶網站這個,請問這:HTTP:// codereview.stackexchange.com/ – NoobEditor 2014-08-29 04:45:28

+1

@NoobEditor爲什麼這不是正確的門戶網站?他有一個編程錯誤,他不知道如何解決。 – 2014-08-29 04:48:29

回答

1

我發現了兩個錯誤,其中一個導致了你的問題,另一個導致了錯誤。首先,你不能在內循環中使用for循環,因爲有時你正在修改你正在循環的數組。因此,在一些迭代中,你隱式地增加y兩次:一次通過實際增加y,並且再次將數組的一部分移到左側。因此,當您不對數組執行更改時,實際上應該增加y,並且在刪除元素時將y保留在原來的位置。其次,你必須確保你不要試圖刪除' ',因爲它會在while循環版本中導致無限遞歸。錯誤修復如下:

public class TestRepeat { 

/** Deletes index index in arr. Elements in (index, arr.length) are shifted to the left, 
    And a ' ' is put at the end of arr 
    Precondition: index >= 0, index < arr.length */ 
private static void deleteAndShift(char[] arr, int index){ 
    for(int i = index; i < arr.length - 1; i++){ 
     arr[i] = arr[i+1]; 
    } 
    arr[arr.length - 1] = ' '; 
} 

public static int deleteRepeats(char[] ch) { 

    int count = 0; 
    for (int x = 0; x < ch.length; x++) { 
     int y = x+1; 
     while(y < ch.length){ 
      //Delete index y. Note that this 'implicitly' increments y by shifting ch. 
      if (ch[x] != ' ' && ch[x] == ch[y]) { 
       deleteAndShift(ch, y); 
      } 
      //Only increment y if an element wasn't deleted 
      else{ 
       y++; 
      } 
     } 
    } 
    for (int q = 0; q < ch.length; q++) { 
     if (ch[q] == ' ') { 
      count++; 
     } 
    } 
    return count;//returns the number of deleted items 
} 

public static void main(String[] args) { 
    char[] ch = {'k', 'a', 'm', 'o', 'k', 'm', 'y', 'm', 'k', 'k', 'x', 'm', 'm', 'o'}; 
    System.out.print("The original array is: "); 
    System.out.println(ch); 
    System.out.println("Number of deleted characters: " + deleteRepeats(ch)); 
    System.out.print("The new array is: "); 
    System.out.println(ch); 
} 

} 

此代碼具有您希望的輸出樣本輸入中刪除8個字符。

+0

非常感謝你的幫助 – 2014-08-29 05:00:34

+1

沒問題!接受並讚揚非常讚賞 – Mshnik 2014-08-29 05:02:05

0

更換if條件塊if (ch[x] == ch[y]) {用,

if (ch[x] != ' ' && ch[x] == ch[y]) {     
    for (int k = y; k < ch.length - 1; k++) {//shifts the array to the right if its a duplicate      
     ch[k] = ch[k + 1]; 
    }      
    ch[ch.length - 1] = ' ';//replaces the last array with a blank 
    y--; 
} 
+0

在for循環中更改for-loop索引變量的值不是很好的做法。 – Mshnik 2014-08-29 05:01:09

+0

@Mshnik可以改變,如果控制很好,循環中的'y ++'或'y - '。 – 2014-08-29 05:11:43

0

後您將它們轉移到左側,做一個y--; 這將有助於照顧連續的重複元素

0

set的幫助下,我們可以輕鬆實現它。

char[] ch = {'k', 'a', 'm', 'o', 'k', 'm', 'y', 'm', 'k', 'k', 'x', 'm', 'm', 'o'}; 
System.out.print("The original array is : "); 
System.out.println(ch); 

// Moving in to LinkedHashSet 
Set<Character> charSet = new LinkedHashSet<Character>(); 
for(char c : ch) 
    charSet.add(c); 

System.out.println("Number of deleted characters :"+(ch.length-charSet.size())); 

// Move Back to newArray 
char[] newch = new char[charSet.size()]; 
int i = 0; 
for(char c : charSet) 
    newch[i++] = c; 

System.out.print("The new array is :"); 
System.out.println(newch); 

輸出:

The original array is : kamokmymkkxmmo 
Number of deleted characters :8 
The new array is :kamoyx