gpt4 book ai didi

java - 泛型以及在实现 Iterable Interface 的类中使用新创建的方法

转载 作者:行者123 更新时间:2023-12-01 23:32:51 24 4
gpt4 key购买 nike

我正在开发一个程序来检查数独解决方案的有效性。我即将完成它,但由于某种原因,当我尝试调用我创建的某些方法时,编译器返回它们未定义。

主要分为三个类:

  1. 一个数独类,它实现了可迭代接口(interface)。它包含一个二维数组,即拼图。构造函数从扫描仪输入中获取一个文件并构建拼图。它有一个迭代器方法来满足接口(interface)要求。此方法返回 SudokuIterator 类型的迭代器。

  2. 数独迭代器类,它实现迭代器接口(interface)。这是 Sudoku 类的私有(private)内部类。它还具有一个二维数组和一个光标作为属性。它具有标准的 hasNext()、next() 和一个remove() stub 来满足该接口(interface)。我还添加了 nextColumn() 和 nextBox(),它们根据光标位置返回一个数组。已重写 next 方法以返回行。

  3. 最后是 validator 。该方法是主要方法。它有一个 isSolution() 方法,该方法根据对 SudokuIterator 类中定义的方法返回的每个数组的分析返回一个 boolean 值。

这就是我的问题出现的地方;当使用迭代器方法实例化并返回 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 来解决此问题(因为这是一个内部类)。

更改:

Iterator<Cell[]> game = puzzle.iterator();

至:

Sudoku.SudokuIterator game = puzzle.iterator();

关于java - 泛型以及在实现 Iterable Interface 的类中使用新创建的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19082036/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com