在底部更新:
我正在创建一个带有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 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
我想要当前说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();
}
}
}
感谢任何帮助。 更新:我的数组的顶行代表这两个字母对:
AA AB AC AD AE AF AG AH AI AI AK A AM A AP A A AR AR AU AV AW AX AY AZ
第二行代表这两个字母对: BA BB BC BD BE BF BG BH BI BJ BK BL BM BN BO BP BQ BR BS 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 :(得分: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;
}
}
答案 1 :(得分: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;
}
}
}
}