Java 查找第一个数字后在二维数组中查找正确的组合
我很难编写这个。。。我想在二维网格中找到一个特定的数字组合。此组合已已知,并存储在ArrayList中。关键在于,这种组合可以从8个基本方向(存储为0到7的整数)中的任何一个方向完成,就像一个单词搜索拼图。我正试图找到那个方向,但老实说,我不知道从这里到哪里去Java 查找第一个数字后在二维数组中查找正确的组合,java,arrays,search,multidimensional-array,Java,Arrays,Search,Multidimensional Array,我很难编写这个。。。我想在二维网格中找到一个特定的数字组合。此组合已已知,并存储在ArrayList中。关键在于,这种组合可以从8个基本方向(存储为0到7的整数)中的任何一个方向完成,就像一个单词搜索拼图。我正试图找到那个方向,但老实说,我不知道从这里到哪里去 [...] //This is only a part of the bigger code I'm working on for (i = 0; i < grid.length; ++i){ for (j = 0; j
[...] //This is only a part of the bigger code I'm working on
for (i = 0; i < grid.length; ++i){
for (j = 0; j < grid[i].length; ++j){
if (grid[i][j] == digits.get(0){ //Here I find the 1st digit of my number
}
}
}
[…]//这只是我正在编写的更大代码的一部分
对于(i=0;i
请记住,输入的数字可以是任意长度,并且只有当我得到完整的数字时,才会返回方向。我真的很迷茫,任何建议都非常感谢,谢谢
for (i = 0; i < grid.length; ++i){
for (j = 0; j < grid[i].length; ++j){
int gridItemIndex = digits.indexOf( grid[i][j] )
{
if( gridItemIndex != -1 )
{
// now you are sure grid[i][j] IS IN LIST
// gridItemIndex tells THE INDEX NUMBER OF grid[i][j] in ArrayList(digits)
}
}
}
处理您试图定位网格[i][j]的存在性和索引号的情况在digits ArrayList中,在没有讨论为什么以及预期组合的最终结果仍然有点模糊之前,请不要质疑我的答案,我很乐意提供帮助我认为这里的问题是你看不到更大的图景。下面是你的出发点,稍微整理一下。 现在我要做的是编写一个方法,在每个方向上检查表的组合长度。检查north基本上就是做grid[startingX][startingY--]。如果你将该逻辑应用到每个方向并解释“越界”错误,你将很快解决它
//2D array
int[][] grid = new int[25][25];
//Your combination
List<Integer> combination = new ArrayList<>();
//Loop over the 2D array to find your starting point
for (int i = 0; i<25; i++) {
for (int j = 0; j<25; j++) {
if (grid[i][j] == combination.get(0)) { //If we find the first element of your combination
//Check in all cardinal directions
checkDirections(i, j);
}
}
}
//二维数组
int[][]网格=新int[25][25];
//你的组合
列表组合=新的ArrayList();
//在二维阵列上循环以找到起点
对于(int i=0;i)首先对一个类进行编码,该类可以在给定的方向上遍历表。例如,top-bottom或bottom-top。然后您可以枚举所有八个可能的方向作为该遍历器,并为每个遍历器检查您的序列。
//2D array
int[][] grid = new int[25][25];
//Your combination
List<Integer> combination = new ArrayList<>();
//Loop over the 2D array to find your starting point
for (int i = 0; i<25; i++) {
for (int j = 0; j<25; j++) {
if (grid[i][j] == combination.get(0)) { //If we find the first element of your combination
//Check in all cardinal directions
checkDirections(i, j);
}
}
}