Java 在方法中递归搜索没有行和列索引的二维数组?
我目前正在做一个家庭作业,我真的很坚持递归搜索数组的想法,没有行和列来索引。我相信我可以使用helper方法,但我对递归还不熟悉,觉得有点困惑。这是不允许我更改的方法(出于作业目的) 我还从讲师那里得到了一个内部课程。我们还没有学到任何关于内部类的知识,但是,内部类似乎没有什么特别之处。它是基本的,我不做任何特殊的事情,所以我不会包含代码,除非有什么需要我不知道的东西。(我不想作弊,我也或多或少地想弄明白)Java 在方法中递归搜索没有行和列索引的二维数组?,java,arrays,recursion,indexing,Java,Arrays,Recursion,Indexing,我目前正在做一个家庭作业,我真的很坚持递归搜索数组的想法,没有行和列来索引。我相信我可以使用helper方法,但我对递归还不熟悉,觉得有点困惑。这是不允许我更改的方法(出于作业目的) 我还从讲师那里得到了一个内部课程。我们还没有学到任何关于内部类的知识,但是,内部类似乎没有什么特别之处。它是基本的,我不做任何特殊的事情,所以我不会包含代码,除非有什么需要我不知道的东西。(我不想作弊,我也或多或少地想弄明白) 编辑:我也不能使用任何循环编辑:删除循环,添加递归示例 递归子程序示例 public c
编辑:我也不能使用任何循环编辑:删除循环,添加递归示例 递归子程序示例
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);
}
}
}
公共类FindElement2DimArrayExample{
私家夫妻{
私人int row;
私人国际学院;
公共耦合(内部行、内部列){
this.row=行;
this.col=col;
}
公共字符串toString(){
返回“[”+行+”,“+列+”];
}
}
公共静态void main(字符串[]args){
int[][]数组=新的int[][{{1,2,3},{4,5,6},{7,8,9};
System.out.println(新的FindElement2DimArrayExample().search(数组,5));
}
公共耦合搜索(int[][]数组,int元素){
递归返回搜索(数组,元素,0,0);
}
公共耦合递归搜索(int[][]数组,int元素,int实际路径,int实际路径){
如果(array.length递归不是搜索2D数组的最佳方法,但如果它是您的赋值,您可以尝试通过“分而治之”的方法实现:将数组分成两部分,并在这两部分上递归调用该方法,直到找到元素为止
也许这对您很有用:您没有指定不能编写其他方法。。。
所以我要写另一个带有签名的搜索方法:
私有耦合搜索(耦合,int[][]数组,int元素)
包含以下内容:
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);
}
并通过以下方式从其他方法调用它:
static public Couple search(int[][] array, int element) {
return search(new Couple(0,0),array,element);
}
这应该能奏效。
除此之外(如果您不能编写其他方法),那么我将使用堆栈
完整代码:
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));
}
}
公共类NewClass1{
静态类偶{
int col,世界其他地区;
公共耦合(内部列,内部行){
this.col=col;
this.row=行;
}
@凌驾
公共字符串toString(){
返回“Couple{”+“col=“+col+”,row=“+row+'}”;
}
}
静态int[]getArr(int-nx,int-ny){
Random rand=新的Random();
int[]arr=新int[nx][ny];
对于(int i=0;i=array.length)返回搜索(新的couple(0,couple.row+1),数组,元素);
if(couple.row>=array[0].length)返回新的couple(-1,-1);
if(数组[couple.row][couple.col]==元素)返回couple;
else返回搜索(新的成对(Couple.col+1,Couple.row),数组,元素);
}
公共静态void main(字符串[]args){
int[]arr=getArr(10,10);
打印(arr);
系统输出println(搜索(arr,11));
}
}
我认为您需要通过递归传递状态,说明您在数组中的位置。但我也不会使用递归在Java中搜索2D数组。@TimBiegeleisen我也会这样做。当我问教授时,他说这是为了学习一些基础知识,更好地理解递归,这是有意义的。我该如何进行传递你确定你为search()
声明的方法签名正确吗?它不应该是公共耦合搜索(int[]]数组,int元素,耦合耦合)
?@user3437460不,它是一个内部方法。编辑“内部类”@Joe根据给定内容,除非允许您访问一些静态变量来记录当前的行
和列
。目前,无法减少问题以达到后续递归调用的目标,因为数组始终是相同的数组,元素始终是相同的elMENT值。唯一的方法是用户Manuel Seiche
所说的,如果你的学校期望这样做,我会感到惊讶,因为它违背了你的方法签名限制的目的。这是我刚刚思考的问题。但是,我应该在问题中更清楚地说明这一点,而不是把它放在一边底部。我不能使用任何类型的循环,只能使用递归。有什么想法吗?@ManuelSeiche编辑:我也不能使用任何循环
@user3437460搜索中没有for循环,只用于填充和打印arr(我假设搜索中没有循环是您需要的).嗯,看来你找到了适合你的解决方案。希望你能取得好成绩!祝你好运。恐怕我找错人了。我不是OP。干杯。
static public Couple search(int[][] array, int element) {
return search(new Couple(0,0),array,element);
}
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));
}
}