如何扫描二维数组?
How to scan a 2d array?
我正在尝试读取文件并在二维数组中填充数据。但是在扫描期间,当我将数据分配到 MazePoint[][]
数组时。它给出以下错误消息。
Type mismatch: cannot convert from int to MazePoint
代码块:
public MazePoint[][] readMaze(String fileToRead) throws IOException
{
FileInputStream fileByteStream = null; // File input stream
Scanner inFS= null; // Scanner object
fileByteStream = new FileInputStream(fileToRead);
inFS= new Scanner(fileByteStream);
int size = inFS.nextInt();
MazePoint[][] maze = new MazePoint[size][size];
while(inFS.hasNext()) {
for (int row = 0; row < size; row++) {
for (int column = 0; column < size; column++) {
maze[row][column] = inFS.nextInt();
}
}
}
return maze;
}
谢谢
MazePoint 是一种新型对象(class)。所以,你不能在那里分配一个整数。但这是可能的。
- 在您的 MazePoint class 中,添加一个新的
value
属性 以及 getter
和 setter
。
class MazePoint {
private int value;
// ... other things here
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
// ... other things
}
- 然后使用setter方法赋值。
public MazePoint[][] readMaze(String fileToRead) throws Exception
{
FileInputStream fileByteStream = new FileInputStream(fileToRead);; // File input stream
Scanner inFS= new Scanner(fileByteStream); // Scanner object
int size = inFS.nextInt();
MazePoint[][] mazePoints = new MazePoint[size][size];
while(inFS.hasNext()) {
for (int row = 0; row < size; row++) {
for (int column = 0; column < size; column++) {
mazePoints[row][column] = new MazePoint();
mazePoints[row][column].setValue(inFS.nextInt());
}
}
}
return mazePoints;
}
完成(演示)项目:
import java.io.FileInputStream;
import java.util.Scanner;
public class Maze {
public static void main(String[] args) throws Exception {
Maze maze = new Maze();
MazePoint[][] mazePoints = maze.readMaze("twoDarray.txt");
maze.print(mazePoints);
}
public MazePoint[][] readMaze(String fileToRead) throws Exception
{
FileInputStream fileByteStream = new FileInputStream(fileToRead);; // File input stream
Scanner inFS= new Scanner(fileByteStream);// Scanner object
int size = inFS.nextInt();
MazePoint[][] mazePoints = new MazePoint[size][size];
while(inFS.hasNext()) {
for (int row = 0; row < size; row++) {
for (int column = 0; column < size; column++) {
mazePoints[row][column] = new MazePoint();
mazePoints[row][column].setValue(inFS.nextInt());
}
}
}
return mazePoints;
}
public void print( MazePoint[][] mazePoints) {
int size = mazePoints.length;
for (int row = 0; row < size; row++) {
for (int column = 0; column < size; column++) {
System.out.print(mazePoints[row][column].getValue() + " ");
}
System.out.println();
}
}
}
class MazePoint {
private int value;
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
}
输入文件twoDarray.txt
:
2 1 2 3 4
输出:
1 2
3 4
我正在尝试读取文件并在二维数组中填充数据。但是在扫描期间,当我将数据分配到 MazePoint[][]
数组时。它给出以下错误消息。
Type mismatch: cannot convert from int to MazePoint
代码块:
public MazePoint[][] readMaze(String fileToRead) throws IOException
{
FileInputStream fileByteStream = null; // File input stream
Scanner inFS= null; // Scanner object
fileByteStream = new FileInputStream(fileToRead);
inFS= new Scanner(fileByteStream);
int size = inFS.nextInt();
MazePoint[][] maze = new MazePoint[size][size];
while(inFS.hasNext()) {
for (int row = 0; row < size; row++) {
for (int column = 0; column < size; column++) {
maze[row][column] = inFS.nextInt();
}
}
}
return maze;
}
谢谢
MazePoint 是一种新型对象(class)。所以,你不能在那里分配一个整数。但这是可能的。
- 在您的 MazePoint class 中,添加一个新的
value
属性 以及getter
和setter
。
class MazePoint {
private int value;
// ... other things here
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
// ... other things
}
- 然后使用setter方法赋值。
public MazePoint[][] readMaze(String fileToRead) throws Exception
{
FileInputStream fileByteStream = new FileInputStream(fileToRead);; // File input stream
Scanner inFS= new Scanner(fileByteStream); // Scanner object
int size = inFS.nextInt();
MazePoint[][] mazePoints = new MazePoint[size][size];
while(inFS.hasNext()) {
for (int row = 0; row < size; row++) {
for (int column = 0; column < size; column++) {
mazePoints[row][column] = new MazePoint();
mazePoints[row][column].setValue(inFS.nextInt());
}
}
}
return mazePoints;
}
完成(演示)项目:
import java.io.FileInputStream;
import java.util.Scanner;
public class Maze {
public static void main(String[] args) throws Exception {
Maze maze = new Maze();
MazePoint[][] mazePoints = maze.readMaze("twoDarray.txt");
maze.print(mazePoints);
}
public MazePoint[][] readMaze(String fileToRead) throws Exception
{
FileInputStream fileByteStream = new FileInputStream(fileToRead);; // File input stream
Scanner inFS= new Scanner(fileByteStream);// Scanner object
int size = inFS.nextInt();
MazePoint[][] mazePoints = new MazePoint[size][size];
while(inFS.hasNext()) {
for (int row = 0; row < size; row++) {
for (int column = 0; column < size; column++) {
mazePoints[row][column] = new MazePoint();
mazePoints[row][column].setValue(inFS.nextInt());
}
}
}
return mazePoints;
}
public void print( MazePoint[][] mazePoints) {
int size = mazePoints.length;
for (int row = 0; row < size; row++) {
for (int column = 0; column < size; column++) {
System.out.print(mazePoints[row][column].getValue() + " ");
}
System.out.println();
}
}
}
class MazePoint {
private int value;
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
}
输入文件twoDarray.txt
:
2 1 2 3 4
输出:
1 2
3 4