Java 2d数组添加一个元素错误

时间:2017-02-16 01:17:41

标签: java arrays multidimensional-array

在我正在处理的程序中,我必须找到包含在未指定数量的输入中的字母对。如果找到两个相同的,不区分大小写的连续英文字母,那么我将一个添加到我的2d数组中的26行乘26列的元素中。这是我的代码:

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 i = 0; i < alphabet.length; i++) {
            for(int j = 0; j < alphabet[i].length; j++) {
                for (int a = 0; a < line.length() - 1; a++) {
                    char firstLetter = line.charAt(a);
                    char secondLetter = line.charAt(a + 1);
                    if (firstLetter == secondLetter) {
                        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();
    }
    }
}

然而,当我运行我的程序并输入&#34; aabbccddeeffgghhiijjkkllmmnnooppqqrrssttuuvvwwxxyyzz&#34; 这就是:

aabbccddeeffgghhiijjkkllmmnnooppqqrrssttuuvvwwxxyyzz
676 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 676 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 676 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 676 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 676 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 676 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 676

我相信它们会被添加到正确的位置,但为什么我的程序会在索引中添加676而不只是添加1?任何帮助是极大的赞赏。谢谢!

2 个答案:

答案 0 :(得分:2)

您的processLine()方法没有多大意义。首先,您应该只迭代输入字符串,而不是整个2D数组,即:

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);
        if (firstLetter == secondLetter) {
            alphabet[firstLetter - 65][secondLetter - 65] += 1;
        }
    }
}

其次,您的2D数组将只在对角线上有条目,因为您只进行两个维度的两个字符相同的分配。所以你可以使用一维数组:

private static int[] alphabet = new int[ROWS];

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);
        if (firstLetter == secondLetter) {
            alphabet[firstLetter - 65] += 1;
        }
    }
}

答案 1 :(得分:1)

你正在做的是你在矩阵上用该位置添加值而不是1,如果你想在那个地方只有1分配它 替换此行

alphabet[firstLetter - 65][secondLetter - 65] += 1;

用这个

alphabet[firstLetter - 65][secondLetter - 65] = 1;

或将您的processLine方法更改为此

 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);
                    if (firstLetter == secondLetter) {
                        alphabet[firstLetter - 65][secondLetter - 65] += 1;
            }
        }
    }

希望这能回答你的问题