【问题标题】:Need help to explain seats at a cinema program made in java需要帮助解释用 java 制作的电影院节目中的座位
【发布时间】:2015-05-12 11:16:43
【问题描述】:

有人可以帮我理解这里发生了什么吗?我发现很难理解这些多维数组发生了什么。如果有人可以详细解释我该程序的作用。我应该在电影院找到第一个可用的座位。该计划将在第一个席位结束。

public class Cinema {




private boolean[][] seats = {{ true, true, false, false, false, true, true false},
                             { true, true, true, true, true, true, true, true},
                             { true, true, true, true, true, true, true, true}}



public void findAvailable() {

    boolean found = false;

    int row = 0;
    while (!found && row < seats.length) {
        int seat = 0;
        while (!found && seat < seats[row].length) {
          if (seats[row][seat]) {
            found = true;
            System.out.println("Row = " + row + " Seat = " + seat);
          }
          seat++;
        }
        row++;
    }
}

【问题讨论】:

    标签: java


    【解决方案1】:

    电影院实际上是解释二维数组的好方法。

    boolean[][] seats = 
    {{true, true, false, false, false, true, true false},
     {true, true, true, true, true, true, true, true},
     {true, true, true, true, true, true, true, true}  }
    

    您可以将阵列中的每一行视为一排座位。现在您可以使用seats[0][0] 使用第一个座位,或者使用seats[0][1] 使用第一排的第二个座位。为了遍历 first 行的所有座位,您可以创建一个 for 循环:

    for(x = 0; x < 8; x++){
        System.out.println("The boolean value of seat " + x + " on the first row is: " + seats[0][x]);
    }
    

    现在您还可以使用 seats[1][x]

    搜索 第二 行(请记住,数组的索引始终从 0 开始)

    现在,如果您想遍历所有可能的座位,则必须进行两个 for 循环:一个循环遍历座位数,另一个循环遍历行:

    for(y = 0; y < 3; y++){
        for(x = 0; x < 8; x++){
            System.out.println("The boolean value of seat " + x + " on row "+ y + " is: " + seats[y][x]);
        }
    }
    

    请注意,您的循环不能超过数组的大小(3 行和 8 个座位)。这就是您使用.length 属性来确定大小的原因。

    现在唯一要做的就是找到第一个可用的座位,所以你再次循环遍历数组,当那个特定座位的布尔值为true时,你必须打破这两个循环。这就是为什么有一个额外的布尔变量found,当找到一个座位时设置为true,这会导致循环不再执行(注意while条件中的!)。

    【讨论】:

      【解决方案2】:

      首先 - 多维数组只不过是数组的数组。 看懂代码,请关注每一行的cmets:

       public class Cinema {
      
      
      
      
         private boolean[][] seats = {{ true, true, false, false, false, true, true false},
                                       { true, true, true, true, true, true, true, true},
                                       { true, true, true, true, true, true, true, true}}
      
      
      
          public void findAvailable() {
      
              boolean found = false; // No available seats found be default
      
              int row = 0; // Number of the row set to 0 by default 
              while (!found && row < seats.length) { // While seat is not found AND row is not exceeding number of all rows 
                  int seat = 0; // Number of seat set to 0 be default 
                  while (!found && seat < seats[row].length) { // While seat is not found AND seat number is not exceeding the number of seats IN the row 
                    if (seats[row][seat]) { // if seat at row number equals to true
                      found = true; // set found to true
                      System.out.println("Row = " + row + " Seat = " + seat);
                    }
                    seat++; // increment seat to next one
                  }
                  row++; // increment row to next one 
              }
          }
      

      A very good explanation of 2D arrays here

      【讨论】:

        【解决方案3】:

        您可以将此算法重写为:

        public void findAvailable() {
        
            boolean found = false;
        
            int row = 0;
            while (!found && row < seats.length) {
                int col= 0;
                while (!found && col< seats[row].length) {
                  if (seats[row][col]) {
                    found = true;
                    System.out.println("Row = " + row + " Seat = " + seat);
                  }
                  col++;
                }
                row++;
            }
        }
        

        我希望它现在是不言自明的。继续从 0 开始在每一行中搜索座位,然后在列中搜索相同的座位。所以搜索从[0,0] 开始直到[n,n],当找到一个座位时,我们就完成了。

        【讨论】:

          【解决方案4】:

          让我们从分解代码开始:

          public class Cinema { ... }
          

          所以,我们在这里声明一个新的数据类型/数据结构,称为 Cinema

          您要声明的这个数据结构,它有一个名为 seats 的私有属性(您将无法从该 Cinema 类 之外访问它),它是ma​​trix of boolean 类型和一个名为 findAvailable 的公共方法(可以从 Cinema 类外部访问),不需要任何参数。

          为了清楚起见,让我们指出任何矩阵都是由一组行组成的,每一行都有它的一组列。此外,我们在 Java 中声明/访问矩阵的方式是:

          // Access; telling which columns and rows we want to access.
          myMatrix[rowIndex][columnIndex];
          // Creation; telling what sizes we want for our matrix.
          DataType[][] variable = new DataType[NumberOfRows][NumberOfColumns];
          

          那么让我们分解一下这些座位的代码,好吗?

          private boolean[][] seats = {{ true, true, false, false, false, true, true false},
                                       { true, true, true, true, true, true, true, true},
                                       { true, true, true, true, true, true, true, true}}
          

          这完全一样:

              private boolean[][] seats = {
                         // This is the instantiation of one row, with 8 columns
                         { true, true, false, false, false, true, true false},
                         // Another row with another 8 more columns (and its values)                                     
                         { true, true, true, true, true, true, true, true},
                         // And another row.                      
                         { true, true, true, true, true, true, true, true}
              };
          

          因此,增加一些间距和缩进看起来会更清晰一些;基本上,您正在创建一个布尔矩阵,每行 3 行 8 列。

          继续讨论该方法,该方法返回 void,即不返回任何内容。

          在该方法中,您正在对矩阵执行简单的搜索:您正在访问每个单元格,以寻找包含“真”值的第一个单元格。

          所以让我们通过注释来检查您的代码。

          public void findAvailable() {
              /* You will use this auxiliar variable to tell or remember if you have found an available spot*/
              boolean found = false;
              /* This variable is your row indexer, an accumulator that you will use to remember which row to access and how many of them have you accessed at a given time*/
              int row = 0;
              /*this translates as follows:
              WHILE I HAVEN'T FOUND (the free spot) AND I HAVEN'T VISITED EACH ROW OF
              SEATS*/
              while (!found && row < seats.length) {
                  /*The same thing, but now it's a column indexer*/
                  int seat = 0;
                  /*this translates as follows:
                  WHILE I HAVEN'T FOUND (the free spot) AND I HAVEN'T VISITED EACH COLUMN
                  OR/OF SEATS*/
                  while (!found && seat < seats[row].length) {
                    // Checking whether the seat at "row, seat" is available
                    if (seats[row][seat]) {
                      // If it is, remember it so we can stop looking any further
                      found = true;
                      // Notify that you've found an empty seat at "row, column"
                      System.out.println("Row = " + row + " Seat = " + seat);
                    }
                    // Keep looking for seats in the same row
                    seat++;
                  }
                  // If no luck on this row, let's move to another one
                  row++;
              }
          
              // if we've reached this point...
              if(!found) System.out.println("There are no seats available this time...");
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2022-01-06
            • 2012-03-31
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多