因此,正如标题所述,我正在尝试将一个程序组合在一起,该程序将比较二维数组的行并确定是否有相同的行。
这必须分两个部分完成;确定是否有任何行是相同的,并确定有多少行是相同的。我正在尝试使用布尔方法确定是否存在任何相同的行,并且尝试使用单独的方法来确定如果布尔方法返回true则有多少行相同。
例如,如果数组为:
1 2 3
2 3 1
1 2 3
然后,布尔方法将返回true,并且输出为:
The matrix has 2 identical rows.
问题是我不知道如何设置布尔方法;如果我能弄清楚那么多,程序的其余部分将很容易弄清楚。
如何为此设置布尔方法?
如果有人需要,这是我到目前为止提出的代码:
public class IdenticalRows {
public static void main(String[] args) {
Scanner KBin = new Scanner(System.in);
char run;
int ROW, COL;
System.out.println("Would you like to begin? (Y/N)");
run = KBin.next().charAt(0);
do {
if(run!='y' && run!='Y' && run!='n' && run!='N') {
System.out.println("Please only type 'y' or 'Y' to begin or type 'n' or 'N' to close the program.");
System.out.println("Would you like to begin? (Y/N)");
run = KBin.next().charAt(0);
}
}while(run!='y' && run!='Y' && run!='n' && run!='N');
do {
System.out.println("Please enter the number of rows:");
ROW = KBin.nextInt();
do {
if (ROW<=0) {
System.out.println("Entry invalid; must be a positive integer greater than 0.");
System.out.println("Please enter the number of rows:");
ROW = KBin.nextInt();
}
}while(ROW<=0);
System.out.println("Please enter the number of columns:");
COL = KBin.nextInt();
do {
if (COL<=0) {
System.out.println("Entry invalid; must be a positive integer greater than 0.");
System.out.println("Please enter the number of columns:");
COL = KBin.nextInt();
}
}while(COL<=0);
int[][] testDuplicates = new int[ROW][COL];
randArray (testDuplicates, MIN, MAX);
System.out.println("Thematrix is:");
printArray(testDuplicates);
System.out.println(" ");
boolean presence = duplicates(testDuplicates);
if (presence=true) {
countDuplicates(testDuplicates);
}
else if (presence=false) {
System.out.println("There are no identical rows in this matrix.");
}
System.out.println("Would you like to run the program again? (Y/N)");
run = KBin.next().charAt(0);
}while(run=='y' || run=='Y');
}
public static void randArray(int[][] matrix, int low, int up) {//Establishes the values of the elements of the array
Random rand = new Random();
for (int r = 0; r < matrix.length; r++) {
for (int c = 0; c < matrix[r].length; c++) {
matrix[r][c] = rand.nextInt(up-low+1)+low;
}
}
}
public static void printArray(int[][] matrix) {//Prints the elements of the array in a square matrix
for (int r = 0; r < matrix.length; r++) {
for (int c = 0; c < matrix[r].length; c++) {
System.out.printf("%5d",matrix[r][c]);
}
System.out.println(" ");
}
}
public static boolean duplicates(int[][] list) {//Determines if any row(s) of elements ever repeat
for (int r=0; r<list.length; r++) {
for (int c=0; c<list[r].length; c++) {
}
}
return false;
}
public static void countDuplicates(int[][] matrix) {
for (int r=0; r<matrix.length; r++) {
for (int c=0; c<matrix[r].length; c++) {
}
}
}
public static final int MAX=3;
public static final int MIN=1;
}
在此先感谢您能提供帮助的人!
答案 0 :(得分:2)
首先编写比较两行的方法:
private static boolean areIdentical(int[] a1, int[] a2) {
if(a1.length != a2.length) return false;
for(int i = 0; i < a1.length; i++) {
if(a1[i] != a2[i]) return false;
}
return true;
}
现在,您可以使用此方法比较行。只需从数组中获取每一行,然后将其与下面的每一行进行比较。类似于下一个:
public static boolean duplicates(int[][] list) {
for (int j = 0; j < list.length; j++) {
if(j == list.length-1) return false; //don't need to compare last row
for (int i = j + 1; i < list.length; i++) {
if(areIdentical(list[j], list[i])) return true;
}
}
return false;
}
您可以轻松地修改此代码以计算重复次数。