Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java中从字符串到二维字符数组_Java_Arrays_String_Char - Fatal编程技术网

Java中从字符串到二维字符数组

Java中从字符串到二维字符数组,java,arrays,string,char,Java,Arrays,String,Char,我想把字符串拼图转换成2D字符数组,就像字谜一样。这是testclass的一部分: public class WordPuzzleTest { WordPuzzle myPuzzle = null; /** * This function will initialize the myPuzzle variable before you start a new test method * @throws Exception */ @Before public void setUp() {

我想把字符串拼图转换成2D字符数组,就像字谜一样。这是testclass的一部分:

public class WordPuzzleTest {

WordPuzzle myPuzzle = null;

/**
 * This function will initialize the myPuzzle variable before you start a new test method
 * @throws Exception
 */
@Before
public void setUp() {
    try {
        this.myPuzzle = new WordPuzzle("VNYBKGSRORANGEETRNXWPLAEALKAPMHNWMRPOCAXBGATNOMEL", 7);
    } catch (IllegalArgumentException ex) {
            System.out.println("An exception has occured");
            System.out.println(ex.getMessage());
    }

}

/**
 * Test the constructor of the {@link WordPuzzle} class
 */
@Test
public void testWordPuzzle() {
        assertNotNull("The object failed to initialize", this.myPuzzle);
        char[][] expectedArray = {{'V','N','Y','B','K','G','S'},
                                 {'R','O','R','A','N','G','E'},
                                 {'E','T','R','N','X','W','P'},
                                 {'L','A','E','A','L','K','A'},
                                 {'P','M','H','N','W','M','R'},
                                 {'P','O','C','A','X','B','G'},
                                 {'A','T','N','O','M','E','L'}};
        assertArrayEquals(expectedArray, this.myPuzzle.getLetterArray());
}
以下是我为此编写的代码,但我得到了以下错误:java.lang.ArrayIndexOutOfBoundsException:0

我不知道这为什么行不通,但我很可能犯了一个愚蠢的错误。有人知道吗

public class WordPuzzle {

    private String puzzle;
    private int numRows;
    private char [][] puzzleArray = new char[numRows][numRows];

    public WordPuzzle(String puzzle, int numRows) {
        super();
        this.puzzle = puzzle;
        this.numRows = numRows;

        char[] puzzleChar;
        puzzleChar=puzzle.toCharArray();

        int index=0;
        int i=0;
        int j=0;
        while (i<numRows) {
            while (j<numRows) {
                puzzleArray[i][j] = puzzleChar[index];
                j++;
                index++;
            }
            i++;
            j=0;
        }   
    }
公共类字谜{
私有字符串拼图;
私人int numRows;
私有字符[][]拼图数组=新字符[numRows][numRows];
公共单词拼图(字符串拼图,整数){
超级();
这个。谜题=谜题;
this.numRows=numRows;
字符[]字符;
puzzchar=puzzle.toCharArray();
int指数=0;
int i=0;
int j=0;

而(i拼图数组的初始值设定项

private char [][] puzzleArray = new char[numRows][numRows];
numRows
为零时,在构造函数之前调用,因此
puzzarray.length==0


只需将
puzzarray=new char[numRows][numRows];
移动到构造函数。

puzzarray的初始值设定项:

private char [][] puzzleArray = new char[numRows][numRows];
private int numRows;
private char [][] puzzleArray = new char[numRows][numRows];
numRows
为零时,在构造函数之前调用,因此
puzzarray.length==0

只需将
puzzarray=newchar[numRows][numRows];
移动到构造函数

private int numRows;
private char [][] puzzleArray = new char[numRows][numRows];
可能是原因。 第一行初始化int,但不定义值,因此该值变为0。 第二行创建一个数组,大小为numRows x numRows,因此为0 x 0。 我想那不是你想要的

可能是原因。 第一行初始化int,但不定义值,因此该值变为0。 第二行创建一个数组,大小为numRows x numRows,因此为0 x 0。
我想这不是你想要的。

我可以建议你用较小的输入来编写测试吗?比如说,2x2或3x3。这比这一大堆“随机”要容易理解得多字母。测试不是我写的,它是给的。但是无论如何,谢谢。我可以建议你用更小的输入来写测试吗?比如说,2x2或3x3。这比这一大堆“随机”字母更容易理解。测试不是我写的,它是给的。但是无论如何,谢谢。