在方法中没有行和列索引的递归搜索2D数组?

时间:2016-03-24 10:11:41

标签: java arrays recursion indexing

我正在做一个家庭作业,我真的坚持这种递归搜索没有行和列索引的数组的想法。我相信我可以使用辅助方法,但我是递归的新手并且发现它有点令人困惑。这是我不允许更改的方法(为了分配目的)

public Couple search(int[][]array, int element){

}

我还提供了教师的内部课程。我们还没有学到任何有关内部类的知识,但是,它似乎并不特别。这是基本的,我没有做任何特别的事情,所以我不会包含代码,除非有一些我不知道需要的东西。 (我不想作弊,我也想或多或少地想出来。)

private class Couple{
  // declaration of (int) row and (int) col

  public Couple(row, col){
  // this.row = row
  // col = col
  }

  public String toString(){
  // returns string
  }

}

编辑:我也不能使用任何循环

3 个答案:

答案 0 :(得分:2)

编辑:删除了for循环,添加了递归示例

递归子程序的例子

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

答案 1 :(得分:0)

递归不是搜索2D数组的最佳方式,但如果是你的任务,你可以尝试通过&#34; Divide and Conquer&#34;来实现它。方法:将数组拆分为两部分,并在这两部分上再次递归调用该方法,直到找到该元素。

也许这对您有用:how to search elements in a 2d array using recursion

答案 2 :(得分:0)

您没有指定您不能写其他方法...... 所以我会用签名编写另一种搜索方法:

私人情侣搜索(情侣情侣,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));

}
}