Searching a 2D Array

29,546

Solution 1

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

Solution 2

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.

Solution 3

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!

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:

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;
}
Share:
29,546
nichi
Author by

nichi

Updated on July 06, 2022

Comments

  • nichi
    nichi almost 2 years

    I have instantiated a 2D array of an editable number of rows and a set number of three columns.

    It is randomly filled with 0's and 1's using the Random .nextInt(2) method.

    After the array is filled, I want to be able to search the array and return the first occurrence of a 0.

    How can I do this?

    For example, if i had an array that looked something like this:

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

    The first occurence would be at (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. 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!

    • James
      James almost 11 years
      You want to return the positions?
    • Andrew Thompson
      Andrew Thompson almost 11 years
      For better help sooner, post an SSCCE.
    • Evans
      Evans almost 11 years
      How do you understand the first occurrence in an 2D array?
    • nichi
      nichi almost 11 years
      @James :Yes, I want to return the position of the first occurence.
    • nichi
      nichi almost 11 years
      @Evans: Sorry about that! I edited my post to make the first occurence clearer!
    • James
      James almost 11 years
      @nichi look at my revised answer, should suit your needs
  • nichi
    nichi almost 11 years
    Yeah, in my Seating class I have actually created 2 different 2D Arrays, one that is numerical and stores the 1's and the 0's shown above, and one that stores the seat numbers such as 1A, 1B, 1C, 2A... My original code returns the seat number of the second array by searching the first array for a 0 and then returning the corresponding seat number in the second array.
  • John484
    John484 almost 11 years
    I guess you could extend what I had further in the Seat class to also hold a meaningful position like 1A etc. You could also create a Venue class made up of a List<Row> and a Row is a List<Seat> and everything could be setup when you create your Venue object.
  • John484
    John484 almost 11 years
    The seat class should really just have an extra method that provides a seat number that a person can understand like 1A etc. by looking at its own row / column values and transforming the columns to letters.
  • nichi
    nichi almost 11 years
    Sorry! I did get it to work :) Thank you very much for your help!