在java构造函数中实例化一个二维数组?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20211744/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 00:20:50  来源:igfitidea点击:

Instantiating a 2d array in a java constructor?

javaarraysconstructor2d

提问by CuriousFellow

I am so confused. I need to make a constructor to create a 2d array with parameters called from main method.. Every time I call the Seats 2D array in another method of the same class, I get an error. Why is that and how do I use the array I made in the constructor?

我感到很困惑。我需要创建一个构造函数来创建一个带有从 main 方法调用的参数的二维数组。每次我在同一个类的另一个方法中调用 Seats 二维数组时,我都会收到一个错误。为什么会这样,我如何使用我在构造函数中创建的数组?

class MovieSeating 
{

public MovieSeating(int rowNum, int columnNum)
{
    String [][] Seats = new String[rowNum][columnNum];
    for (int r = 0; r < rowNum; r++)
    {
        for (int c = 0; c < columnNum; c++)
        {
            Seats[r][c] = "???";
        }
    }
}

private Customer getCustomerAt(int row, int col)
{
    System.out.println("Customer at row " + row + " and col " + col + "." );
    System.out.println(Seats[row][col]);

}

回答by Narendra Pathai

class MovieSeating{

private String[][] seats;

public MovieSeating(int col, int row){
  seats = new String[row][col];
}
}

Make seats an instance variableto increase the scope of it.

使座椅成为一个实例变量以增加它的范围。

回答by Maroun

Seatsis not known in the main, it's known only in the scope of the constructor.

Seats在 中不知道,main它只在构造函数的范围内知道。

You should make it a class memberand initialize it in the constructor:

您应该使其成为类成员并在构造函数中对其进行初始化:

class MovieSeating  {
   private String [][] Seats;
   ..
   public MovieSeating(int rowNum, int columnNum) {
      Seats = new String[rowNum][columnNum];
      ..
   }
}

回答by user987339

You are on a good track, but you have to make Seatsinstance variable in order to get proper results:

您处于良好的轨道上,但您必须使Seats实例变量才能获得正确的结果:

private String [][] Seats;
public MovieSeating(int rowNum, int columnNum)
{
    Seats = new String[rowNum][columnNum];
    for (int r = 0; r < rowNum; r++)
    {
        for (int c = 0; c < columnNum; c++)
        {
            Seats[r][c] = "???";
        }
    }
}

回答by Nishant Lakhara

Declare the array outside the constructor as a private variable :

将构造函数外的数组声明为私有变量:

class MovieSeating 
{
    private String [][] Seats;
public MovieSeating(int rowNum, int columnNum)
{
    Seats = new String[rowNum][columnNum];
    for (int r = 0; r < rowNum; r++)
    {
        for (int c = 0; c < columnNum; c++)
        {
            Seats[r][c] = "???";
        }
    }
}

private void getCustomerAt(int row, int col)
{
    System.out.println("Customer at row " + row + " and col " + col + "." );
    System.out.println(Seats[row][col]);

}
}

回答by Chandu D

Here i am sending code snipped i hope this will help u

在这里,我正在发送代码片段,希望这对您有所帮助

public Charts(int graph_min, int graph_max, double[] dataset, int stepSize,double[][] percentRange) {

//here double[][]={{10},{100}};
  this(graph_min,graph_max,dataset);
  this.stepSize = stepSize;
  
  System.out.println("double constructor    "+percentRange.length);
  this.percentRange = new double[percentRange.length][percentRange[0].length];
  System.out.println("percentRange: "+this.percentRange);
   for (int i = 0; i < percentRange.length; i++) {
   
         for (int j = 0; j < percentRange[i].length; j++) {
              this.percentRange[i][j] = percentRange[i][j];
               System.out.println("ps_Axis constructor valuesd  "+this.percentRange[i][j]);
            }
        
 }