使用 int 二维数组来计算 Java 中的 char 数组
Use int 2d array to count char array in Java
我还没做完,named getRows() 方法中的问题,我只是尝试使用一个 int [][]counts 来计算 char [][] 数组每行有多少 X,然后打印出来。我做错了,没有工作。
package labassignment6;
import java.util.Random;
public class LabAssignment6 {
public static void main(String[] args) {
int size;
char array[][];
Random randy = new Random();
for (size = 4; size <= 6; size++) {
//fill the array with random X O or space
array = fillArray(size, randy);
//print array
System.out.println("\nsize" + size);
printArray(array);
doRows(array);
// 1.method to doRows which call
// -method with loops to count X and Os in each row
// -methos print each row's counts
//doColumns(array);
// 2.method to doColumns which call
// -method with loops to count per column
// -method to print each column's counts
//doDiagonals(array);
// 3.method to doDiagonals which calls
// -method with single loop to count top left to bottom right and
// another sigle loop to count top right to bottom left
// -method to print diagonal's counts
}//end outer array size loop
}
public static char[][] fillArray(int size, Random randy) {
int row = 0, col = 0, N;
char i = ' ';
char array[][];
// create array
array = new char[size][size];
//code inside loops to choose X, O or space with weighting
for (row = 0; row < size; row++) {
for (col = 0; col < size; col++) {
N = randy.nextInt(5);
if (N == 0 || N == 1) {
i = 'X';
} else if (N == 2 || N == 3) {
i = 'O';
} else {
i = ' ';
}
array[row][col] = i;
}
System.out.println();
}
return array;
}
public static void printArray(char array[][]) {
int row, col;
for (row = 0; row < array.length; row++) {
for (col = 0; col < array[row].length; col++) {
System.out.print(array[row][col] + " ");
}
System.out.println();
}
}
public static void doRows(char array[][]) {
int counts[][];
//counts = new int [0][2];
counts = new int[array.length - 1][array.length - 1];
getRows(array, counts);
// printRows(array, counts);
}
public static void getRows(char array[][], int counts[][]) {
// int x = 0, o = 0, s = 0;
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[0].length; j++) {
if (i<array[j].length && array[i][j] == 'X') {
counts[i][0]++;
}
}
}
for (int i = 0; i < array.length; i++) {
System.out.println("Row " + i + " has " + counts[i][1] + " X");
}
}
我已经解决了您的 ArrayIndexOutOfBounds 异常 - 这是由以下原因引起的:
counts = new int[array.length - 1][array.length - 1];
计数数组大小设置得太小。它需要与原始数组的长度相同,否则最终会变得太小。
不过正如ajb所说,count[][]其实应该是一个一维数组。如果你看一下你的代码,你会发现你只使用了 count[i][0] - 所以基本上,你使用它几乎就像它是一个一维数组一样 - 因为你只使用一个索引i 的每个值。所以你可以只使用 count[i] 而不是 count[i][0] ,它会做同样的事情。二维方法当然可以工作,并且可以教您使用二维数组的有趣方法。但一般来说,更简单的方法往往是更好的方法。 K.I.S.S。 :)
你的问题主要出在getRows函数上。您在内部循环的每次迭代中增加 counts[i][0] 的值,但是当输出每行中的 X 数时,您使用值 counts[i][1] - 它永远不会设置为任何值(因为你只使用 counts[i][0],而不是 counts[i][1])并且默认为 0。顺便说一句,在内循环的 if 语句中,您不需要检查 i < length。外部 for 循环已经检查了该条件,并且 i 没有在循环内的任何地方递增,它仅在每个循环执行后递增 1 - 然后测试条件检查它是否小于 array.length再次。所以在 if 语句中检查它是多余的 - 它总是 return true 除非你在循环体内的某个地方增加 i 。
这是 getRows 的工作版本:
public static void getRows(char array[][], int counts[][]) {
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[0].length; j++) {
if (array[i][j] == 'X') {
counts[i][0]++;
}
}
}
for (int i = 0; i < array.length; i++) {
System.out.println("Row " + i + " has " + counts[i][0] + " X");
}
}
但是,如您所见,您确实应该使用一维数组 - 即使该程序确实可以正常运行。我将很快使用一维数组重新发布整个程序。应该只需要一分钟。
我想我会把程序的完整工作代码给他。我希望这能回答问题:
package labassignment6;
import java.util.*;
public class LabAssignment6 {
public static void main(String[] args) {
int size;
char array[][];
Random randy = new Random();
for (size = 4; size <= 6; size++) {
//fill the array with random X O or space
array = fillArray(size, randy);
//print array
System.out.println("\nsize: " + size);
printArray(array);
doRows(array);
}
}
public static char[][] fillArray(int size, Random randy) {
int row = 0, col = 0, N;
char i = ' ';
char array[][];
// create array
array = new char[size][size];
for (row = 0; row < size; row++) {
for (col = 0; col < size; col++) {
N = randy.nextInt(5);
if (N == 0 || N == 1) {
i = 'X';
} else if (N == 2 || N == 3) {
i = 'O';
} else {
i = ' ';
}
array[row][col] = i;
}
System.out.println();
}
return array;
}
public static void printArray(char array[][]) {
int row, col;
for (row = 0; row < array.length; row++) {
for (col = 0; col < array[row].length; col++) {
System.out.print(array[row][col] + " ");
}
System.out.println();
}
}
public static void doRows(char array[][]) {
int counts[];
counts = new int[array.length];
getRows(array, counts);
}
public static void getRows(char array[][], int counts[]) {
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[0].length; j++) {
if (array[i][j] == 'X') {
counts[i]++;
}
}
}
for (int i = 0; i < array.length; i++) {
System.out.println("Row " + i + " has " + counts[i] + " X");
}
}
}
我还没做完,named getRows() 方法中的问题,我只是尝试使用一个 int [][]counts 来计算 char [][] 数组每行有多少 X,然后打印出来。我做错了,没有工作。
package labassignment6;
import java.util.Random;
public class LabAssignment6 {
public static void main(String[] args) {
int size;
char array[][];
Random randy = new Random();
for (size = 4; size <= 6; size++) {
//fill the array with random X O or space
array = fillArray(size, randy);
//print array
System.out.println("\nsize" + size);
printArray(array);
doRows(array);
// 1.method to doRows which call
// -method with loops to count X and Os in each row
// -methos print each row's counts
//doColumns(array);
// 2.method to doColumns which call
// -method with loops to count per column
// -method to print each column's counts
//doDiagonals(array);
// 3.method to doDiagonals which calls
// -method with single loop to count top left to bottom right and
// another sigle loop to count top right to bottom left
// -method to print diagonal's counts
}//end outer array size loop
}
public static char[][] fillArray(int size, Random randy) {
int row = 0, col = 0, N;
char i = ' ';
char array[][];
// create array
array = new char[size][size];
//code inside loops to choose X, O or space with weighting
for (row = 0; row < size; row++) {
for (col = 0; col < size; col++) {
N = randy.nextInt(5);
if (N == 0 || N == 1) {
i = 'X';
} else if (N == 2 || N == 3) {
i = 'O';
} else {
i = ' ';
}
array[row][col] = i;
}
System.out.println();
}
return array;
}
public static void printArray(char array[][]) {
int row, col;
for (row = 0; row < array.length; row++) {
for (col = 0; col < array[row].length; col++) {
System.out.print(array[row][col] + " ");
}
System.out.println();
}
}
public static void doRows(char array[][]) {
int counts[][];
//counts = new int [0][2];
counts = new int[array.length - 1][array.length - 1];
getRows(array, counts);
// printRows(array, counts);
}
public static void getRows(char array[][], int counts[][]) {
// int x = 0, o = 0, s = 0;
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[0].length; j++) {
if (i<array[j].length && array[i][j] == 'X') {
counts[i][0]++;
}
}
}
for (int i = 0; i < array.length; i++) {
System.out.println("Row " + i + " has " + counts[i][1] + " X");
}
}
我已经解决了您的 ArrayIndexOutOfBounds 异常 - 这是由以下原因引起的:
counts = new int[array.length - 1][array.length - 1];
计数数组大小设置得太小。它需要与原始数组的长度相同,否则最终会变得太小。
不过正如ajb所说,count[][]其实应该是一个一维数组。如果你看一下你的代码,你会发现你只使用了 count[i][0] - 所以基本上,你使用它几乎就像它是一个一维数组一样 - 因为你只使用一个索引i 的每个值。所以你可以只使用 count[i] 而不是 count[i][0] ,它会做同样的事情。二维方法当然可以工作,并且可以教您使用二维数组的有趣方法。但一般来说,更简单的方法往往是更好的方法。 K.I.S.S。 :)
你的问题主要出在getRows函数上。您在内部循环的每次迭代中增加 counts[i][0] 的值,但是当输出每行中的 X 数时,您使用值 counts[i][1] - 它永远不会设置为任何值(因为你只使用 counts[i][0],而不是 counts[i][1])并且默认为 0。顺便说一句,在内循环的 if 语句中,您不需要检查 i < length。外部 for 循环已经检查了该条件,并且 i 没有在循环内的任何地方递增,它仅在每个循环执行后递增 1 - 然后测试条件检查它是否小于 array.length再次。所以在 if 语句中检查它是多余的 - 它总是 return true 除非你在循环体内的某个地方增加 i 。
这是 getRows 的工作版本:
public static void getRows(char array[][], int counts[][]) {
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[0].length; j++) {
if (array[i][j] == 'X') {
counts[i][0]++;
}
}
}
for (int i = 0; i < array.length; i++) {
System.out.println("Row " + i + " has " + counts[i][0] + " X");
}
}
但是,如您所见,您确实应该使用一维数组 - 即使该程序确实可以正常运行。我将很快使用一维数组重新发布整个程序。应该只需要一分钟。
我想我会把程序的完整工作代码给他。我希望这能回答问题:
package labassignment6;
import java.util.*;
public class LabAssignment6 {
public static void main(String[] args) {
int size;
char array[][];
Random randy = new Random();
for (size = 4; size <= 6; size++) {
//fill the array with random X O or space
array = fillArray(size, randy);
//print array
System.out.println("\nsize: " + size);
printArray(array);
doRows(array);
}
}
public static char[][] fillArray(int size, Random randy) {
int row = 0, col = 0, N;
char i = ' ';
char array[][];
// create array
array = new char[size][size];
for (row = 0; row < size; row++) {
for (col = 0; col < size; col++) {
N = randy.nextInt(5);
if (N == 0 || N == 1) {
i = 'X';
} else if (N == 2 || N == 3) {
i = 'O';
} else {
i = ' ';
}
array[row][col] = i;
}
System.out.println();
}
return array;
}
public static void printArray(char array[][]) {
int row, col;
for (row = 0; row < array.length; row++) {
for (col = 0; col < array[row].length; col++) {
System.out.print(array[row][col] + " ");
}
System.out.println();
}
}
public static void doRows(char array[][]) {
int counts[];
counts = new int[array.length];
getRows(array, counts);
}
public static void getRows(char array[][], int counts[]) {
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[0].length; j++) {
if (array[i][j] == 'X') {
counts[i]++;
}
}
}
for (int i = 0; i < array.length; i++) {
System.out.println("Row " + i + " has " + counts[i] + " X");
}
}
}