[英]Generics and using newly created methods in a Class that implements Iterable Interface
我正在开发一个用于检查数独解决方案有效性的程序。 我快要完成它了,但是由于某种原因,当我尝试调用已创建的某些方法时,编译器会返回它们未定义的信息。
主要分为三类:
数独类,它实现可迭代的接口。 它包含一个二维数组,这是一个难题。 构造函数从扫描仪输入中获取文件并构建拼图。 它具有迭代器方法来满足接口要求。 此方法返回SudokuIterator类型的插入器。
一个SudokuIterator类,该类实现Iterator接口。 这是Sudoku类的私有内部类。 它还具有一个二维数组和一个游标作为属性。 它具有标准的hasNext(),next()和remove()存根(stub)来满足该接口。 我还添加了一个nextColumn()和nextBox(),它们根据光标位置返回一个数组。 下一个方法已被覆盖以返回行。
最后是验证器。 此方法是主要方法。 它具有isSolution()方法,该方法根据对从SudokuIterator类中定义的方法返回的每个数组的分析返回一个布尔值。
这就是我出现问题的地方。 当使用迭代器方法实例化并返回SudokuIterator并尝试使用添加的nextColumn()和nextBox()方法时,编译器将返回未为Iterator定义的方法。
任何意见或建议将不胜感激。 提前致谢。
import java.util.*;
/**
* Sudoku class represents the matrix of cells in a Sudoku puzzle
* @version 01/05/2012
* @author Bob Wilson
*/
public class Sudoku implements Iterable<Cell []>
{
private Cell [] [] puzzle;
/**
* Default constructor should not be called. Make it private.
*/
private Sudoku() {}
/**
* Puzzle constructor that uses a Scanner object to read a file.
* File contains 81 numbers that are the values of the 81 cells.
* @param file a Scanner object on a File object
*/
public Sudoku(Scanner file)
{
int size = file.nextInt();
System.out.println("Size: " + size);
puzzle = new Cell[size][size];
for (int i = 0; i < size; i++)
for (int j = 0; j < size; j++)
puzzle[i][j] = new Cell(file.nextInt());
}
public int getLength(){
return this.puzzle.length;
}
class SudokuIterator implements Iterator<Cell []> {
private Cell [][] puzzle;
private int cursor;
public SudokuIterator(Cell [][] puzzle){
this.puzzle = puzzle;
cursor = 1;
}
public boolean hasNext(){
if(cursor <= this.puzzle.length){
return true;
}
return false;
}
public Cell[] next(){
Cell[] row = puzzle[cursor-1];
cursor++;
return row;
}
public Cell[] nextColumn(){
Cell[] column = new Cell[puzzle.length];
for(int i = 0; i < puzzle.length; i++){
column[i] = puzzle[i][cursor-1];
}
cursor++;
return column;
}
public Cell[] nextBox(){
Cell[] box = new Cell[puzzle.length];
int boxIndex = 0;
for(int i = ((cursor - 1)/((int)Math.sqrt(puzzle.length)))*(int)Math.sqrt(puzzle.length) ; i < ((cursor - 1)/((int)Math.sqrt(puzzle.length)))*(int)Math.sqrt(puzzle.length) + (int)Math.sqrt(puzzle.length); i++){
for(int j = (((cursor - 1) + ((int)Math.sqrt(puzzle.length))) % ((int)Math.sqrt(puzzle.length))) * ((int)Math.sqrt(puzzle.length)); j < (((cursor - 1) + ((int)Math.sqrt(puzzle.length))) % ((int)Math.sqrt(puzzle.length))) * ((int)Math.sqrt(puzzle.length)) + ((int)Math.sqrt(puzzle.length)); j++){
box[boxIndex] = puzzle[i][j];
}
}
cursor++;
return box;
}
public void remove(){}
}
/**
* Generates and returns a String representation of the puzzle cells
* @return A String representing the contents of the puzzle array
*/
public String toString()
{
// display the puzzle
String value = "Puzzle is:\n";
for (int i = 0; i < puzzle.length; i++) {
for (int j = 0; j < puzzle[i].length; j++)
value += puzzle[i][j].toString();
value += "\n";
}
return value;
}
/**
* Instantiates and returns a new SudokuIterator object
* @return A SudokuIterator object on the puzzle array
*/
public SudokuIterator iterator(){
SudokuIterator iterator = new SudokuIterator(this.puzzle);
return iterator;
}
}
/* 201340 */
import java.util.*;
import java.io.*;
/**
* This class instantiates a Sudoku object passing a Scanner on a
* file to the Sudoku constructor. It prints the puzzle using the
* Sudoku toString method. It determines if the digit matrix is a
* valid solution for a Sudoku puzzle or not and prints the result.
*
* @version 01/05/2012
* @author Bob Wilson
*
*/
public class SudokuValidator
{
private Sudoku puzzle;
/**
* @param args - not used
*/
public static void main( String [] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("Please enter name of file containing puzzle to verify");
SudokuValidator myValidator = new SudokuValidator(scan.nextLine());
System.out.println(myValidator.isSolution());
}
public SudokuValidator(String fileName)
{
Scanner file = null;
try
{
file = new Scanner(new File(fileName));
}
catch (Exception e)
{
System.out.println("Bad file name");
System.exit(0);
}
puzzle = new Sudoku(file);
System.out.println(puzzle);
}
public boolean isSolution(){
boolean flag = true;
Iterator<Cell[]> game = puzzle.iterator();
while(game.hasNext()){
Cell[] row = game.next();
Cell[] column = game.nextColumn();
Cell[] box = game.nextBox();
for(Cell i: row){
for(Cell j: row){
if(j.equals(i.getValue())){
flag = false;
return flag;
}
}
}
}
return flag;
}
} /* 201340 */
问题是, game
引用变量的声明类型是Iterator
,它没有定义任何nextColumn()
方法,因此编译器无法找到它。 您可以通过将声明的类型更改为Sudoku.SudokuIterator
来解决此Sudoku.SudokuIterator
(因为这是一个内部类)。
更改:
Iterator<Cell[]> game = puzzle.iterator();
至:
Sudoku.SudokuIterator game = puzzle.iterator();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.