简体   繁体   English

如何在不设置维度的情况下初始化二维数组?

[英]How do you initialize a 2D array without setting its dimensions?

I am using a method which reads in a file and sets the size of a 2D array based on the first 3 lines.我正在使用一种读取文件并根据前 3 行设置二维数组大小的方法。 My method returns a Room[][] object and the method also sets the dimensions of the Room[][] object so that I cannot set it outside before doing the method.我的方法返回一个Room[][]对象,该方法还设置了Room[][]对象的尺寸,因此在执行该方法之前我无法将其设置在外部。 For example:例如:

public Room[][] readRooms(String filepath) throws IOException
    {
        int numberOfRooms;
        int numRows;
        int numCols;
        Room[][] grid;

    try
    {
        FileReader fr = new FileReader(filepath);
        BufferedReader br = new BufferedReader(fr);
        String next = null;

        numberOfRooms = Integer.parseInt(br.readLine());
        numRows = Integer.parseInt(br.readLine());
        numCols = Integer.parseInt(br.readLine());

        grid = new Room[numRows][numCols];

        while((next = br.readLine()) != null)
        {
            //do stuff
        }
    }
    catch{ //catch stuff }

return grid;
}

What I am attempting is to create a new Room[][] object in a main method in another class that does this:我正在尝试的是在另一个执行此操作的类的 main 方法中创建一个新的 Room[][] 对象:

Room[][] maze = readRooms(myfilepath);

However, Java is requiring that maze be defined before I can use it like this.但是,Java 要求在我可以像这样使用它之前定义迷宫。 Is it not possible to initialize a 2D array without explicitly declaring the dimensions?是否无法在不明确声明维度的情况下初始化 2D 数组?

Edit: The main method for those asking:编辑:那些询问的主要方法:

public static void main(String args[])
{
    Room[][] maze;
    maze = readRooms("C:/Users/Blaise/Programming/csc300/Maze");
}

The error is your function readRooms is not static .错误是您的函数readRooms不是static Make it static and you will not need an instance of Room to call it.将其设为static ,您将不需要Room的实例来调用它。 But you do need to handle the IOException in your caller (or add throws IOException to main() )但是您确实需要在调用者中处理IOException (或将throws IOException添加到main()

public static Room[][] readRooms(String filepath) throws IOException

Also, you cannot double declare maze 's type.此外,您不能双重声明maze的类型。

public static void main(String args[])
{
  Room[][] maze;
  try {
    maze = Room.readRooms("C:/Users/Blaise/Programming/csc300/Maze");
  } catch (IOException e) {
    e.printStackTrace();
  }
}

or或者

public static void main(String args[]) throws IOException
{
  // Room[][] maze; // <-- not
  Room[][] maze = Room.readRooms("C:/Users/Blaise/Programming/csc300/Maze");
}

An array's size is always fixed in Java.数组的大小在 Java 中总是固定的。 So you can't....Typically instead of using an array, you'd use an implementation of List here - usually ArrayList, but with plenty of other alternatives available.所以你不能......通常不使用数组,而是在这里使用 List 的实现 - 通常是 ArrayList,但有很多其他可用的替代方法。

You can create an array from the list as a final step, of course - or just change the signature of the method to return a List to start with当然,您可以从列表中创建一个数组作为最后一步 - 或者只是更改方法的签名以返回一个列表以开始

Your main needs to either throw an IOException or surround maze = readRooms(myfilepath) with:您主要需要抛出一个 IOException 或围绕maze = readRooms(myfilepath)

try {...} catch (IOException) {...}

Also you're declaring maze twice.你也两次宣布迷宫。 Instead of writing而不是写作

Room[][] maze;
Room[][] maze = readRooms(myfilepath);

Shorten it to将其缩短为

Room[][] maze = readRooms(myfilepath);

Like Eran said.就像埃兰说的那样。 Or if you really don't like that, do:或者,如果您真的不喜欢那样,请执行以下操作:

Room[][] maze;
maze = readRooms(myfilepath);

and Elliot is right too, readRooms() needs to be static. Elliot 也是对的, readRooms()需要是静态的。

I don't know if I answered your question or not, but at least it's some constructive criticism :).我不知道我是否回答了你的问题,但至少这是一些建设性的批评:)。

Edit : I noticed you have try {...} catch {...} in your readRooms method... is this used to catch an IOException?编辑:我注意到您在 readRooms 方法中try {...} catch {...} ...这是用来捕获 IOException 的吗? If so, readRooms() doesn't need to throw an IOException at all.如果是这样, readRooms()根本不需要抛出 IOException 。

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

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