2017-02-16 130 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 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 

我想,目前说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