我试图在Java中反序列化单个变量,但是在投射值时似乎遇到了问题:
public void loadGrid(Sudoku testGrid) {
try {
FileInputStream fileIn = new FileInputStream(System.getProperty("user.home") + "\\tmp\\grid.grd");
ObjectInputStream in = new ObjectInputStream(fileIn);
testGrid.sudokuGrid = (Sudoku.sudokuGrid) in.readObject();
in.close();
fileIn.close();
} catch(IOException i) {
i.printStackTrace();
} catch(ClassNotFoundException c) {
c.printStackTrace();
}
}
Sudoku是一个包含ArrayList变量的ArrayList的类。
我碰到了:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Sudoku.sudokuGrid cannot be resolved to a type
最佳答案
您需要转换为Sudoku.sudokuGrid
的类型,而不是sudokuGrid
字段本身:
testGrid.sudokuGrid = (ArrayList<ArrayList<SomeType>>) in.readObject();