简体   繁体   English

搜索二维数组

[英]Searching a 2D Array

I have instantiated a 2D array of an editable number of rows and a set number of three columns. 我实例化了一个2D数组,该数组具有可编辑数量的行和固定数量的三列。

It is randomly filled with 0's and 1's using the Random .nextInt(2) method. 使用Random .nextInt(2)方法将其随机填充为0和1。

After the array is filled, I want to be able to search the array and return the first occurrence of a 0. 数组填充后,我希望能够搜索数组并返回第一次出现的0。

How can I do this? 我怎样才能做到这一点?

For example, if i had an array that looked something like this: 例如,如果我有一个看起来像这样的数组:

  • 1 1 0 1 1 0
  • 0 1 0 0 1 0
  • 1 1 1 1 1 1

The first occurence would be at (0,3). 第一次出现在(0,3)。 I want to search the array horizontally and when it reaches the third column (the end), it will go to the next row. 我想水平搜索数组,当它到达第三列(末尾)时,它将转到下一行。

Note: I originally tested the following section of code with a 2D array that was completely filled with 0's and when I manually inserted 1's in the array and then tried to search for the first occurence of a 0 it worked. 注意:最初,我用完全充满0的2D数组测试了以下代码部分,当我在数组中手动​​插入1并尝试搜索首次出现的0时,它起作用了。 However, the code doesn't work when the array is randomly filled.. 但是,当随机填充数组时,代码不起作用。

 public String findNextAvailable()
{ 
    for (int i=0; i<seatlist.length; i++) 
    { 
        for (int j=0; j<seatlist[i].length; j++)
        {

            int k=0;
            if (seatlist[0][0]==0) 
            { 
                nextavailable= seatchart[0][0];
                break;
            }
            else
            if(seatlist[k][j]==0)
            {
                nextavailable= seatchart[k][j];
                break;
            }
            else 
            {   k++;
                if(seatlist[k][j]==0) 
                {
                    nextavailable= seatchart[k][j];
                    break;
                }    
            }

        }
    }
    return nextavailable;
}

Thanks in advance! 提前致谢!

for (int i = 0; i < seats.length; i++) {
        for (int j = 0; j < seats[i].length; j++) {
            if (seats[i][j] == 0) {
                return "Next available seat at position: [" + i + "][" + j + "]";
            }
        }
 }
 return "No seat available";

Although you might want to create a seat object instead that is easier to work with: 尽管您可能想创建一个座位对象,但是使用起来更容易:

public class Seat {

    private int row;
    private int column;

    public Seat(int row, int column){
        this.row = row;
        this.column = column;
    }
    public int getRow() {
        return row;
    }

    public int getColumn() {
        return column;
    }
}

and replace the returning of a string with: 并将返回的字符串替换为:

return new Seat(i,j);

well when you break in the inner loop, you still execute again the outer loop and you wind up replacing what you think is your final result by the next run of the outer loop. 好的,当您在内部循环中中断时,仍然可以再次执行外部循环,然后最终将您认为是最终结果的内容替换为下一轮外部循环。 rather than use break, just return right there. 而不是使用break,只需返回就可以了。

You need to return the positions of the first encountered 0, so why are you breaking out of the if statement, the outer loop will still run! 您需要返回遇到的第一个0的位置,因此为什么要中断if语句,外部循环仍将运行!

Simply create an integer array: 只需创建一个整数数组:

int[] pos=new array[2];

Change the return type: 更改返回类型:

public int[] findNextAvailable(){

In each of the if statements change the contents so that it reads: 在每个if语句中更改内容,使其内容如下:

pos[0]=i;
pos[1]=j;
return pos;

The end result will look something like this: 最终结果将如下所示:

    public int[] findNextAvailable()
{ 
    int[] pos=new array[2];
    for (int i=0; i<seatlist.length; i++) 
    { 
        for (int j=0; j<seatlist[i].length; j++)
        {

            if (seatlist[i][j]==0) 
            { 
                pos[0]=i;
                pos[1]=j;
                return pos;
            }


        }
    }

   //none found so return minus one.
    pos[0]=-1;
    pos[1]=-1;

    return pos;
}

there are many different types of searches, some faster and some easier to do. 搜索的类型很多,有些更快,有些更容易实现。 Here's a program i made that has methods for all types of them. 这是我制作的程序,其中包含针对所有类型的方法。 you will have to modify them a bit so it will search a 2d array but it shouldn't be too hard. 您将需要对其进行一些修改,以便它可以搜索2d数组,但应该不会太难。 ' package linear_search; 'package linear_search; import java.util.Arrays; 导入java.util.Arrays; import java.util.Scanner; 导入java.util.Scanner;

public class Linear_search {
int[] array = {10,12,42,7,22,1,3,4,5,9};
int ans;
int num;
int min;
int max;
void start(){
    arraySort(array);
    dump(array);
    Scanner scan = new Scanner(System.in);
    System.out.println("Enter a value to search:");
    num = scan.nextInt();
    ans = recursiveBinarySearch(array, 0, array.length-1);
    if(ans == -1){
        System.out.println("Your value was not found");
    } else {
    System.out.println("Your value was found at position " + ans);
    }
    }
    void dump(int[] array){
     for(int i = 0; i < array.length ; i++){
         System.out.print(array[i] + " ");
     }
     System.out.println();
     }
     int linearsearch(int[] array){
     for(int i = 0; i < array.length; i++){
         if(array[i] == num){
         return i;
     }
     }
     return -1;
     }
     int binarysearch(int[] array){
     min = 0;
     max = array.length -1;
     int mid = (min + max) / 2;

     while(array[mid] != num){
     if(num > array[mid]){
         min = mid+1;
         mid = (min + max) / 2;
     }
     if(num < array[mid]){
         max = mid-1;
         mid = (min + mid) / 2;
     }
     if(min == max && array[mid] != num){
         return -1;
     }
     }
     return mid;
     }
     int recursiveBinarySearch(int[] array, int min, int max){
     int mid = (min + max) / 2;
     if(array[mid] == num){
         return mid;
     } 
     if(min == max && array[mid] != num){
         return -1;
     }
     if(num > array[mid]){
         return recursiveBinarySearch(array, mid+1, max);
     }
     if(num < array[mid]){
         return recursiveBinarySearch(array, min, mid-1);
     }
     return mid;
     }
     void arraySort(int[] a){
    Arrays.sort(array);
    }
     public static void main(String[] args) {
     Linear_search main = new Linear_search();
     main.start();
    } 
}

' you will just have to remove the scanner and hard code in "0" for the default value you should search for. '您只需要删除扫描仪和“ 0”中的硬编码,即可获取应搜索的默认值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM