【问题标题】:Java – Working with 2 dimensional array and constructor (Beginner)Java – 使用二维数组和构造函数(初学者)
【发布时间】:2018-05-27 16:56:59
【问题描述】:

这篇文章会很长,但如果社区可以帮助我,我会很高兴。

这是以前课堂测试中的一道题。这次我不得不期待一个类似的,我现在被困住了。我是 Java 及其概念的新手。 测试结果应该是这样的:

The result

我同时面临多个问题。我会尽量做到具体。首先我将解释这个任务,它给出了不能改变的条件。

任务 创建一个二维数组字段。该字段应适用于各种尺寸。除大小外,数组字段应包含以下模式(见图)。

1.) 创建一个构造函数,该构造函数根据传入的参数创建一个矩形数组字段,并带有额外的条件。

1.1) 如果参数小于5,则该字段应强制显示为5/5字段。

1.2) 如果参数大于 5,它应该始终显示为 n/n 数组。

public class Pattern {
 char[][] field;

public Pattern(int n) {
  // Here goes my code for creating the field
}

2.) 编写一个填充构造函数的方法。

public void fillArray() {
  // Here goes my code.
}

我目前的方法

public Pattern(int n) {
field = new char[n][n];              
int i, j;

if (field.length < 5) {
    for (i = 1; i <= 5; i++) {
        for (j = 1; j <= 5; j++) {
            // ? what to do with the array here?
        }
        System.out.println();
    }
}


public void fillArray() {
// no fu**ing idea... 
} 


public static void main (String[]args){
    Pattern cross = new Pattern(2);  
    cross.fillArray();
    System.out.println(cross);
}

问题

1.) 在构造函数中应该返回什么值?

2.) 如何访问方法中的对象并设置一个 for 循环,以获取预定义的字段大小?我应该使用this 吗?

很抱歉我对如何传递数组信息并正确执行这些信息有不好的理解。

【问题讨论】:

    标签: java arrays cons


    【解决方案1】:
    public class Pattern {
    
        private final int size;
        private final char[][] field;
    
        public Pattern(int n) {
            size = Math.max(n, 5);
            field = new char[size][size];
        }
    
        public void fillArray() {
            for (int row = 0, i = size - 1; row < size; row++, i--) {
                for (int col = 0, j = size - 1; col < size; col++, j--) {
                    if (row == col || row == j)
                        field[row][col] = '*';
                    else if (col > row)
                        field[row][col] = col < i ? '1' : '2';
                    else
                        field[row][col] = col > i ? '3' : '4';
                }
            }
        }
    
        public void print() {
            for (int row = 0; row < size; row++) {
                for (int col = 0; col < size; col++)
                    System.out.print(field[row][col] == '\0' ? ' ' : field[row][col]);
                System.out.println();
            }
        }
    
    
        public static void main(String... args) {
            Pattern cross = new Pattern(2);
            cross.fillArray();
            cross.print();
        }
    
    }
    

    【讨论】:

      【解决方案2】:

      您将 fillArray 中的内容放入构造函数中。构造函数应该是这样的:

      public Pattern(int n) {
          if (n < 5) {
              field = new char[5][5];
          } else {
              field = new char[n][n];
          }
      }
      

      并且两个for循环应该在fillArray方法中:

      public void fillArray() {
          // you should loop until field.length and field[x].length
          for (int x = 0; x < field.length; x++) {
              for (int y = 0; y < field[x].length; y++) {
                  // ...
              }
          }
      }
      

      尝试编写自己的填充数组的逻辑。


      如果你被卡住了,这是我的解决方案:

      for (int x = 0 ; x < field.length ; x++) {
          for (int y = 0 ; y < field[x].length ; y++) {
              /*
              The '1' area can be represented by these inequalities:
              x - y < 0
              x + y < array.length - 1
              The '2' area can be represented by these inequalities:
              x - y < 0
              x + y > array.length - 1
              The '3' area can be represented by these inequalities:
              x - y > 0
              x + y > array.length - 1
              The '4' area can be represented by these inequalities:
              x - y > 0
              x + y < array.length - 1
              Everywhere that does not satisfy any of the inequalities are *s
               */
              int xMinusY = x - y;
              int xPlusY = x + y;
              if (xMinusY < 0 && xPlusY < field.length - 1) {
                  field[x][y] = '1';
              } else if (xMinusY < 0 && xPlusY > field.length - 1) {
                  field[x][y] = '2';
              } else if (xMinusY > 0 && xPlusY > field.length - 1) {
                  field[x][y] = '3';
              } else if (xMinusY > 0 && xPlusY < field.length - 1) {
                  field[x][y] = '4';
              } else {
                  field[x][y] = '*';
              }
          }
      }
      
      // ...
      
      // printing the array out:
      
      for (int x = 0 ; x < field.length ; x++) {
          for (int y = 0 ; y < field.length ; y++) {
              System.out.print(field[x][y]);
          }
          System.out.println();
      }
      

      【讨论】:

      • 非常感谢!我已经用你的解决方案解决了这个任务!
      【解决方案3】:

      回答您的问题:

      1. 构造函数不返回任何内容。

      2. 您不需要使用“this”,因为二维数组是可以在类 Pattern 中的任何位置访问的字段。如果要访问数组的大小,可以使用field.length。

      在填充数组之前,应该先检查n是否小于5(如果是,则应设置为5)。这是在构造函数中完成的。然后创建数组。

      在那之后,您有 for 循环,您必须在其中弄清楚如何创建模式。这应该在 fillArray 方法中完成。

      【讨论】:

      • 感谢您的解释!
      【解决方案4】:

      好的,这就是解决方案。要得到这种图案,先在纸上画出来,然后写下列和行,然后搜索图案。

      你可以通过实践来学习这一点。

      构造函数类似于方法,但没有返回值并使用类的名称。 当您实例化该类的新对象时,将始终调用构造函数。

      示例:

      Pattern p = new Pattern(3);
      

      将构造函数留给创建该类的对象时需要做的事情。根据您的情况决定您的 Pattern 的大小。

      示例:

      public Pattern(int fS) { //fS = fieldSize
        if (fS < 5)
            fS = 5;
        field = new char[fS][fS];
      }
      

      然后你就有了方法,一旦创建了一个对象,你就可以使用他的实例中的所有方法。在您的情况下,您将有一个名为“fillArray”的方法。

      解决方案:

          public void fillArray() {
          int fS = field.length;
          char c = ' ';
          int len = fS - 1;
      
          for (int row = 0; row < fS; row++)
              for (int col = 0; col < fS; col++) {
                  if (row == col || row + col == len) c = '*';// This will make the asterisc cross
                  if (row < col && row < len - col) c = '1'; //This will do the 1 part
                  if (row < col && row > len - col) c = '2'; //This will do the 2 part
                  if (row > col && row > len - col) c = '3';//This will do the 3 part
                  if (row > col && row < len - col) c = '4';//This will do the 4 part
      
                  field[row][col] = c;
              }
      }
      

      记住: 在这种情况下,您的数组将不会被填充,直到您调用方法 fillArray()

      示例:

      Pattern p = new Pattern(7);
      p.fillArray();
      

      要打印您的 Pattern,我建议您使用 toString() 方法。您将需要覆盖它。

      如何:

      @Override //Important
      public String toString() {
          StringBuilder s = new StringBuilder();
          for (char[] row : field) {
              for (char col : row)
                  s.append("\t").append(col);
              s.append("\n");
          }
          return s.toString();
      }
      

      使用“@Override”标签查看您在编写 toString 方法时是否有任何拼写错误。现在打印你的模式使用:

          Pattern p = new Pattern(7);
          p.fillArray();
          System.out.println(p); //This line
      

      PD:对不起,如果我犯了任何语法错误,我的母语不是英语。希望对您有所帮助。

      【讨论】:

        猜你喜欢
        • 2016-07-28
        • 2013-02-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-02-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多