比较Java中的二维数组
我试图找出给定的第二个数组在第一个数组中的位置,但似乎无法转换为字符串来找出它们的位置:比较Java中的二维数组,java,arrays,Java,Arrays,我试图找出给定的第二个数组在第一个数组中的位置,但似乎无法转换为字符串来找出它们的位置: public static void main(String[] args) { char i [][] = new char[][]{ {'d', 's', 'l', 'e', 'i', 'g', 'h', 'e', 'i', 'j', 'a', 's', 'l', 'd', 'k', 'j'}, {'a', 'b', 'c', 'd', 'e',
public static void main(String[] args) {
char i [][] = new char[][]{
{'d', 's', 'l', 'e', 'i', 'g', 'h', 'e', 'i', 'j', 'a', 's', 'l', 'd', 'k', 'j'},
{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'W', 'w', 'Z', 'Z', 'Z', 'W', '1', 'l', 'k'},
{'h', 'i', 'j', 'k', 'l', 'm', 'n', 'Z', 'A', 'a', 'Z', 'a', 'Z', '2', 'i', 'n'},
{'o', 'p', 'q', 'r', 's', 't', 'u', 'Z', 'Z', 'L', 'l', 'Z', 'Z', '3', 'i', 'v'},
{'v', 'w', 'x', 'y', 'z', '1', '2', 'Z', 'd', 'Z', 'D', 'd', 'Z', '4', 'q', 'i'},
{'3', '4', '5', '6', '7', '8', '9', 'o', 'Z', 'Z', 'o', 'O', 'Z', '5', 'b', 'v'},
{'k', 'e', '8', '7', '8', '4', 'j', 'f', 'l', 'k', 'a', 'v', '8', '8', 'h', 'j'}
};
char w [][] = new char[][]{
{'W', 'w', '.', '.', '.', 'W', '1'},
{'.', 'A', 'a', '.', 'a', '.', '2'},
{'.', '.', 'L', 'l', '.', '.', '3'},
{'.', 'd', '.', 'D', 'd', '.', '4'},
{'o', '.', '.', 'o', 'O', '.', '5'}
};
find(i, w);
}
public static int[] find(char [][] image, char [][] waldo) {
for (int i = 0; i < waldo.length; i++) {
char[] largerCharArray= large[i];
String largerString = new String(largerCharArray);
//used for debug purposes
char[] array = largerCharArray;
char [] smallCharArray = small[i];
String smallString = new String(smallCharArray);
char[] array1 = smallCharArray;
//beginning comparison
if (largerString.indexOf(smallString) >= 0) {
int temp = largerString.indexOf(smallString);
}
//debug purposes
System.out.println(largerString.indexOf(smallString));
System.out.println(Arrays.toString(array));
System.out.println(Arrays.toString(array1));
}
//for debug purposes
return null;
}
publicstaticvoidmain(字符串[]args){
字符i[][]=新字符[][]{
{'d','s','l','e','i','g','h','e','i','j','a','s','l','d','k','j'},
{'a','b','c','d','e','f','g','W','W','Z','Z','Z','W','1','l','k'},
{'h','i','j','k','l','m','n','Z','A','A','Z','2','i','n'},
{'o','p','q','r','s','t','u','Z','Z','L','L','Z','Z','3','i','v'},
{'v','w','x','y','z','1','2','z','d','z','d','d','z','4','q','i'},
{'3','4','5','6','7','8','9','o','Z','Z','o','o','Z','5','b','v'},
{'k','e','8','7','8','4','j','f','l','k','a','v','8','8','h','j'}
};
字符w[][]=新字符[][]{
{'W','W','W','W','W','1'},
{'.','A','A','A','A','A','2'},
{.'.','.',L',L','.','.','.','3'},
{'.','d'.','d','d','d','4'.},
{'o','o','o','o','o','5'}
};
发现(i,w);
}
公共静态int[]find(char[]image,char[]waldo){
for(int i=0;i=0){
int temp=largerString.indexOf(smallString);
}
//调试目的
System.out.println(largerString.indexOf(smallString));
System.out.println(array.toString(array));
System.out.println(Arrays.toString(array1));
}
//用于调试目的
返回null;
}
据我所知,任务是找到匹配的子矩阵。如果用通配符替换点,这是有意义的。因此,较小的矩阵位于从1开始计数的位置(行=2,列=8)
下面是搜索子矩阵的代码。我使用正则表达式将行匹配为字符串:
public static int[] find(char [][] large, char [][] small) {
outer: for (int i = 0; i < large.length - small.length; i++) {
int column = -1;
for (int j = 0; j < small.length ; j++) {
String largerString = new String(large[i + j]);
String smallString = new String(small[j]);
Matcher matcher = Pattern.compile(smallString).matcher(largerString);
if(!matcher.find()){
continue outer;
}
int thisColumn = matcher.start();
if(column != -1 && column != thisColumn){
continue outer;
}
column = thisColumn;
}
System.out.printf("found it: %d, %d!", i, column);
}
//for debug purposes
return null;
}
publicstaticint[]find(char[]large,char[]small){
外部:用于(int i=0;i
试试我刚写完的这段代码,它可能有一些bug,它会尝试改进它。它不使用字符串
private static int[] find(char[][] image, char[][] waldo) {
int row = -1;
int column = -1;
char first_waldo = waldo[0][0]; // first char of waldo
boolean continue1 = true; // this is used to avoid looping when the indexes tried are not a possible answer
int size = waldo.length * waldo[0].length; // number of elements of waldo
int cont = 0;
for (int i = 0; i < image.length; i++) {
for (int j = 0; j < image[i].length; j++) { // Looping over chars in image array
char current = image[i][j]; // Current char of image array
if (current == first_waldo) { // If true, this indexes (i, j) are a possible answer
row = i; // Current image row
column = j; // Current image column
cont = 0; // Count of how many chars are matched
for (int k = row; k < row + waldo.length; k++) {
for (int l = column; l < column + waldo[0].length; l++) { // Looping over
// the possible matching array in image
// System.out.println("Comparing " + image[k][l] + " with " + waldo[k - row][l - column]);
// System.out.println(k + " " + l);
if (waldo[k - row][l - column] == '.') { // If its a point, count as matching characters
cont++;
continue;
}
if (image[k][l] != waldo[k - row][l - column]) { // If chars are different, it's not the answer
row = -1;
column = -1;
continue1 = false; // So it doesn't continue looping
break;
} else {
cont++; // If they are equals, count as matching characters
}
}
if (continue1 == false) { // To avoid overlooping when it's not the answer
continue1 = true; // Reset value
break;
}
}
if (cont == size) { // Is the number of matched charactes equal to the size of waldo?
int[] res = {row, column};
return res; // Return indexes
}
} else {
row = -1;
column = -1;
}
}
}
int[] res = {-1, -1};
// System.out.println("\n" + row +" , " + column);
return res; // Not answer
}
private static int[]find(char[]image,char[]waldo){
int行=-1;
int列=-1;
char first_waldo=waldo[0][0];//waldo的第一个字符
boolean continue1=true;//当尝试的索引不是可能的答案时,这用于避免循环
int size=waldo.length*waldo[0].length;//waldo的元素数
int cont=0;
对于(int i=0;i
问题是什么?小数组的定义在哪里?他表达的唯一问题是