[英]Recursively search 2D array without row and column indexing in method?
I'm currently working on a homework assignment and I'm really stuck on this idea of recursively searching an array without a row and column to index. 我目前正在从事一项家庭作业,而我真的坚持这种递归搜索数组的想法,而无需在行和列之间建立索引。 I believe I can use helper methods but I'm new to recursion and find it a little confusing.
我相信我可以使用辅助方法,但是我是递归的新手,觉得有点困惑。 This is the method that I'm not allowed to change (for the purpose of the assignment)
这是我不允许更改的方法(出于分配目的)
public Couple search(int[][]array, int element){
}
I'm also provided an inner class from the instructor. 我还提供了讲师的内部课程。 We haven't learned anything about inner classes, however, the appear to be nothing special.
我们还没有了解内部类的任何知识,但是看起来没什么特别的。 It is basic and I doesn't do anything special so I won't include the code unless there is something that is needed that I don't know.
这是基本的,我没有做任何特别的事情,因此除非有我不知道的需要,否则我不会包含代码。 (I don't want to be cheating and I want to more-or-less figure it out too).
(我不想作弊,我也想或多或少地弄清楚这一点)。
private class Couple{
// declaration of (int) row and (int) col
public Couple(row, col){
// this.row = row
// col = col
}
public String toString(){
// returns string
}
}
EDIT: I also can't use any loops 编辑:我也不能使用任何循环
Edit: removed for-loop, added recursive Example 编辑:删除了循环,添加了递归示例
example for recursive subroutine 递归子程序示例
public class FindElement2DimArrayExample {
private class Couple {
private int row;
private int col;
public Couple(int row, int col) {
this.row = row;
this.col = col;
}
public String toString() {
return "[" + row + ", " + col + "]";
}
}
public static void main(String[] args) {
int[][] array = new int[][] { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
System.out.println(new FindElement2DimArrayExample().search(array, 5));
}
public Couple search(int[][] array, int element) {
return searchRecursively(array, element, 0, 0);
}
public Couple searchRecursively(int[][] array, int element, int actualRow, int actualCol) {
if (array.length <= actualRow) {
return null;
} else if (array[actualRow].length <= actualCol) {
return searchRecursively(array, element, actualRow + 1, 0);
} else if (array[actualRow][actualCol] == element) {
return new Couple(actualRow, actualCol);
} else {
return searchRecursively(array, element, actualRow, actualCol + 1);
}
}
}
Recursion is not the best way to search a 2D Array, but if it is your assignment, you may try to realize it via the "Divide and Conquer" approach: Split the array into two parts and recursively call the method again on these two parts until you found the element. 递归不是搜索2D数组的最佳方法,但是如果是您的任务,则可以尝试通过“分而治之”的方法来实现:将数组分为两部分,然后在这两个部分上再次递归调用该方法直到找到元素。
Maybe this is useful for you: how to search elements in a 2d array using recursion 也许这对您有用: 如何使用递归搜索二维数组中的元素
You didn't specify that you cant write some other method... So i'd write another search method with signature: 您未指定您无法编写其他方法...因此,我将编写另一个带有签名的搜索方法:
private Couple search(Couple couple, int[][] array, int element) 私人夫妻搜索(夫妻对,int [] []数组,int元素)
Containing the following: 包含以下内容:
static private Couple search(Couple couple, int[][] array, int element) {
try {
System.out.println("Checking:" + couple + (array[couple.col][couple.row]));
} catch (ArrayIndexOutOfBoundsException aioobe) {}
if (couple.col>=array.length) return search(new Couple(0,couple.row+1),array,element);
if (couple.row>=array[0].length) return new Couple(-1,-1);
if (array[couple.row][couple.col] == element) return couple;
else return search(new Couple(couple.col+1,couple.row),array,element);
}
and call it from your other method by: 并通过以下方法从其他方法调用它:
static public Couple search(int[][] array, int element) {
return search(new Couple(0,0),array,element);
}
That should do the trick. 这应该够了吧。 Other then that (if you cant write additional method) then I'd use a stack.
除此之外(如果您不能编写其他方法),那么我将使用堆栈。
full code: 完整代码:
public class NewClass1 {
static class Couple {
int col, row;
public Couple(int col, int row) {
this.col = col;
this.row = row;
}
@Override
public String toString() {
return "Couple{" + "col=" + col + ", row=" + row + '}';
}
}
static int[][] getArr(int nx, int ny) {
Random rand = new Random();
int[][] arr = new int[nx][ny];
for (int i = 0; i < nx; i++) {
for (int j = 0; j < ny; j++) {
arr[i][j] = rand.nextInt(90)+10;
}
}
return arr;
}
static void print(int [][] arr) {
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
System.out.print(arr[i][j] + ";");
}
System.out.println("");
}
}
static public Couple search(int[][] array, int element) {
return search(new Couple(0,0),array,element);
}
static private Couple search(Couple couple, int[][] array, int element) {
try {
System.out.println("Checking:" + couple + (array[couple.col][couple.row]));
} catch (ArrayIndexOutOfBoundsException aioobe) {}
if (couple.col>=array.length) return search(new Couple(0,couple.row+1),array,element);
if (couple.row>=array[0].length) return new Couple(-1,-1);
if (array[couple.row][couple.col] == element) return couple;
else return search(new Couple(couple.col+1,couple.row),array,element);
}
public static void main(String[] args) {
int[][] arr = getArr(10,10);
print(arr);
System.out.println(search(arr,11));
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.