gpt4 book ai didi

java - 比较 2D 数组 Java

转载 作者:行者123 更新时间:2023-12-01 04:14:06 24 4
gpt4 key购买 nike

我试图找出给定的第二个数组将位于第一个数组中的位置,但似乎无法将其转换为字符串以找出它们的位置:

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;
}

最佳答案

尝试一下我刚刚完成的这段代码,它可能有一些错误,它会尽力改进它。它不使用字符串。

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

}

关于java - 比较 2D 数组 Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19613240/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com