2017-02-16 124 views
1

更新在底部:Java 2d陣列邏輯錯誤

我正在用2d數組創建程序。該數組有26行,26列。我正在一次搜索不區分大小寫的雙字母對,如:「AA」,「AB」,「AC」,「AD」等。如果找到一對字母,我添加一個它是數組中的相應索引。第一個字母確定該行,第二個字母確定該列。例如,這是它應該如何運行:

aa 
ab 
ac 
ad 
ba 
bb 
bc 
za 
zb 
zc 
1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0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 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 

我想,目前說4左上數量最多,換句話說就是2.換句話說,我希望任何特定的字母對都是,每行只能計算一次。所以,如果我輸入AAAAAAAAAAA,我想只添加一個以在字母[0] [0] 這裏是我的代碼:

import java.util.Scanner; 

public class Freq{ 
    private static final int ROWS = 26; 
    private static final int COLS = 26; 
    private static int[] [] alphabet = new int[ROWS][COLS]; 
    public static void main(String[] args) { 
     String line; 
     Scanner userInput = new Scanner(System.in); 
     while(userInput.hasNextLine()) { 
      line = userInput.nextLine(); 
      processLine(line); 

     } 
     printArray(); 

    } 
    public static void processLine(String line) { 
     line = line.toUpperCase(); 
     for(int a = 0; a < line.length() - 1; a++) { 
      char firstLetter = line.charAt(a); 
      char secondLetter = line.charAt(a + 1); 
      alphabet[firstLetter - 65][secondLetter - 65] += 1; 

     } 
    } 
    public static void printArray() { 
    for (int b = 0; b < alphabet.length; b++) { 
     for (int c = 0; c < alphabet[b].length; c++){ 
      System.out.print(alphabet[b][c] + " "); 
     } 
     System.out.println(); 
    } 
    } 
} 

任何幫助表示讚賞。 UPDATE:我的數組的頂行代表這兩個字母對:

AA AB AC AD AE AF AG AH AI AJ AK AL是一個AO AP AQ AR AS AT AU AV AW AX AY AZ

第二行表示這兩個字母對: BA BB BC BD BE BF BG BH BI BJ BK BL BM BN BO BP BQ BS BR BT BU BV BW BX BY BZ

等 字符串:「這是一個測試「有這些字母對:TH,HI,IS,IS,TE,ES,ST。這些都是該字符串中的所有字母對。但是,我只想對每個字母對進行一次計數,所以出於我的目的,我想將該字符串記錄爲具有這些字母對:TH,HI,IS,TE,ES,ST。請注意IS沒有列出了兩次,因爲我只是在尋找每個輸入的一行出現

這意味着,如果我在一行輸入:

AAAAAAAAAAAA

我只想增加1到我的數組中的第一個元素。所以,我希望我的數組的頂行看起來像這樣:

1 0 0 0 0 0 0 ...

然而,如果我輸入多行這樣的字符串「AAAAAAAAAAAA」 :

AA
AA
AA
AA
AA
AA
AA

然後我想添加6到我的數組的第一個元素。 因爲AA在6條不同線上發生6次不同的行爲所以我想讓我的數組的最上一行看起來像這樣:

6 0 0 0 0 0 0 ...

+0

在這裏數組可能不是一個好的選擇,因爲寬度是固定的,並且實際上你不知道最終會有多少匹配。 –

+0

寬度不需要變化。我所做的只是添加到數組的一個元素。根據我找到的比賽數量,我不需要創建更多索引。如果我找到一個匹配,我只需將它添加到它的相應元素。我不知道該怎麼做的事情是,如果我在一條線上有不止一個事件,我只想添加一個,但不會超過一個。 – Coder117

+0

我給你的答案在下面,我認爲應該在這裏工作。 –

回答

1

下面的實現增加了一個二維布爾陣列稱爲visited以跟蹤一對給定的字母是否已經看到當前行 。如果我們遇到了一對字母,對於我們以前從未見過的給定行,則我們爲該單元添加一行。但在當前行中再次看到同一對將被忽略,總數不會向上移動。消耗完每一行後,我們將visited數組重置爲false,以便它可以重複使用後續行。

public class Freq { 
    private static final int ROWS = 26; 
    private static final int COLS = 26; 
    private static int[][] alphabet = new int[ROWS][COLS]; 
    private static boolean[][] visited = new boolean[ROWS][COLS]; 

    public static void main(String[] args) { 
     String line; 
     Scanner userInput = new Scanner(System.in); 
     while (userInput.hasNextLine()) { 
      clearVisited(); 
      line = userInput.nextLine(); 
      processLine(line); 
     } 
     printArray(); 
    } 

    public static void clearVisited() { 
     for (int r=0; r < visited.length; r++) { 
      for (int c=0; c < visited[r].length; c++){ 
       visited[r][c] = false; 
      } 
     } 
    } 

    public static void processLine(String line) { 
     line = line.toUpperCase(); 
     for (int i=0; i < line.length()-1; i++) { 
      char firstLetter = line.charAt(i); 
      char secondLetter = line.charAt(i + 1); 
      if (!visited[firstLetter-65][secondLetter-65]) { 
       alphabet[firstLetter - 65][secondLetter - 65] += 1; 
       visited[firstLetter-65][secondLetter-65] = true; 
      } 
     } 
    } 
} 
2

只要把條件在這裏:

for (int a=0; a < line.length() - 1; a++) { 
    char firstLetter = line.charAt(a); 
    char secondLetter = line.charAt(a + 1); 
    if (alphabet[firstLetter - 65][secondLetter - 65] == 0) { // Put it here 
     alphabet[firstLetter - 65][secondLetter - 65] = 1; 
    } 
} 
+0

如果幫助其他人是正確的,請將支票放在我的回答中。謝謝! –

+0

但是,如果我輸入:「AA」,「AA」和「AABBAA」在三條不同的線上,我仍然只有一個值在我的第一個元素,但我需要有一個值3. – Coder117

+0

我應該再次提問,看看有沒有人回覆? – Coder117