简体   繁体   English

Java 二维数组

[英]Java Two-Dimensional Array

The two-dimensional array of boolean values that will be referenced by matrix will be used to simulate an LED-based display that can hold LETTERS_PER_DISPLAY letters.将由矩阵引用的 boolean 值的二维数组将用于模拟可容纳 LETTERS_PER_DISPLAY 字母的基于 LED 的显示器。

Modify the constructor to create a two-dimensional array of boolean that has FONT_LETTER_HEIGHT rows and (FONT_LETTER_WIDTH times LETTERS_PER_DISPLAY) columns and assign it to the instance variable matrix.修改构造函数以创建具有 FONT_LETTER_HEIGHT 行和 (FONT_LETTER_WIDTH 乘以 LETTERS_PER_DISPLAY) 列的 boolean 的二维数组,并将其分配给实例变量矩阵。

public class LEDDisplay
{
   private boolean[] = matrix;
   private static final int FONT_LETTER_HEIGHT = 5;
   private static final int FONT_LETTER_WIDTH = 6;
   private static final int LETTERS_PER_DISPLAY = 10;

public LEDDisplay()
{
    boolean[][] matrix = new boolean[FONT_LETTER_HEIGHT][FONT_LETTER_WIDTH]
}

Please could you tell me whether my constructor is correct?请你告诉我我的构造函数是否正确?

it's actually not correct - you hide the member variable matrix by defining a local to the constructor one.这实际上是不正确的 - 您通过定义构造函数的局部变量来隐藏成员变量矩阵。 Here is the correct way:这是正确的方法:

public class LEDDisplay
{
   private boolean[][] matrix;
   private static final int FONT_LETTER_HEIGHT = 5;
   private static final int FONT_LETTER_WIDTH = 6;
   private static final int LETTERS_PER_DISPLAY = 10;

public LEDDisplay()
{
    matrix = new boolean[FONT_LETTER_HEIGHT][FONT_LETTER_WIDTH * LETTERS_PER_DISPLAY];
}

Your one dimentional array matrix is class variable whereas matrix in constructor is local to the constructor and not visible outside the constructor.您的一维数组矩阵是 class 变量,而构造函数中的矩阵对于构造函数是本地的,并且在构造函数之外不可见。

FONT_LETTER_HEIGHT rows and (FONT_LETTER_WIDTH times LETTERS_PER_DISPLAY) columns FONT_LETTER_HEIGHT 行和(FONT_LETTER_WIDTH乘以LETTERS_PER_DISPLAY)列

That should be:那应该是:

new boolean[FONT_LETTER_HEIGHT][FONT_LETTER_WIDTH * LETTERS_PER_DISPLAY]

and assign it to the instance variable matrix.并将其分配给实例变量矩阵。

public LEDDisplay()
{
    matrix = new boolean[FONT_LETTER_HEIGHT][FONT_LETTER_WIDTH]
}

However然而

boolean[][] matrix = new boolean[FONT_LETTER_HEIGHT][FONT_LETTER_WIDTH]

creates a local variable matrix that is visible only in the constructor.创建一个仅在构造函数中可见的局部变量matrix This will have no effect on the instance variable matrix .这对实例变量matrix没有影响。

Two small probs with the code:代码的两个小问题:

  1. matrix is defined as a member variable of the class, should be defined as a 2-D array, and then does not need to be re-defined in the constructor矩阵定义为class的成员变量,应该定义为二维数组,然后不需要在构造函数中重新定义

  2. the original post states that there should be原来的帖子说应该有

"(FONT_LETTER_WIDTH times LETTERS_PER_DISPLAY) columns" “(FONT_LETTER_WIDTH 乘以 LETTERS_PER_DISPLAY)列”

and your constructor only includes FONT_LETTER_WIDTH并且您的构造函数仅包含 FONT_LETTER_WIDTH

Something like this:像这样的东西:

public class LEDDisplay
{
   private boolean[][] matrix;
   private static final int FONT_LETTER_HEIGHT = 5;
   private static final int FONT_LETTER_WIDTH = 6;
   private static final int LETTERS_PER_DISPLAY = 10;

public LEDDisplay()
{
    matrix = new boolean[FONT_LETTER_HEIGHT][FONT_LETTER_WIDTH * LETTERS_PER_DISPLAY];
}

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

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