파일을 읽고 처음 세 줄을 기준으로 2D 배열의 크기를 설정하는 방법을 사용하고 있습니다. 내 메서드는 Room[][]
개체를 반환하며이 메서드는 Room[][]
개체의 크기를 설정하므로 메서드를 수행하기 전에 외부로 설정할 수 없습니다. 예를 들어 :치수를 설정하지 않고 2D 배열을 어떻게 초기화합니까?
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;
}
내가 시도하고하는 것은이없는 다른 클래스의 main 메소드에 새로운 룸 [] [] 객체를 생성하는 것입니다 그러나
Room[][] maze = readRooms(myfilepath);
을, 자바는 그 미로를 요구하고 내가 이것을 이렇게 사용할 수 있기 전에 정의되어야한다. 명시 적으로 차원을 선언하지 않고 2D 배열을 초기화 할 수 있습니까?
편집 : 그 묻는위한 주요 방법 :
public static void main(String args[])
{
Room[][] maze;
maze = readRooms("C:/Users/Blaise/Programming/csc300/Maze");
}
'maze'가 정의 된 코드를 보여주고, 선호하면 [MCVE] (http://stackoverflow.com/help/mcve). 또한 정확한 오류 메시지를 알려주십시오. – Radiodef