0

所以我有一个由用户估算的字符串数组列表。我还有一个二维字符数组:私人字符[][]拼图;我需要帮助将字符串更改为字符,以便我可以将它们输入到二维数组中有人可以帮忙吗?

public class WordSearchPuzzle
{
    private char[][] puzzle ;
    private ArrayList<String> puzzleWords ;
    private int letterCount = 0 ;
    private int gridDimensions;

    public WordSearchPuzzle(ArrayList<String> userSpecifiedWords)
    {
        this.puzzleWords = userSpecifiedWords ;

    }

    private void createPuzzleGrid()
    {
        int i;
        for(i = 0; i < puzzleWords.size() ; i++){
            letterCount = puzzleWords.size() + letterCount ;
        }
        gridDimensions = letterCount * 2;
        puzzle = new char[gridDimensions][gridDimensions] ;
    }
4

3 回答 3

1

将每个字符串更改为charArray

for(String item : ArrayList){
  item.toCharArray();
}
于 2012-04-17T11:12:55.750 回答
0
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class WordSearchPuzzle {

    private char[][] puzzle ;
    private List<String> puzzleWords; //note: don't use a specific implementation (e.g. ArrayList) when you define a List  

    public WordSearchPuzzle(List<String> userSpecifiedWords)
    {
        this.puzzleWords = userSpecifiedWords ;

    }

    public void createPuzzleGrid()
    {
        puzzle = new char[puzzleWords.size()][];
        for(int i = 0; i < puzzleWords.size() ; i++){
            puzzle[i] = puzzleWords.get(i).toCharArray();
        }
    }   


    public void debugPuzzle(){
        System.out.println(Arrays.deepToString(puzzle));
    }
    /**
     * @param args
     */
    public static void main(String[] args) {
        List<String> userWords = new ArrayList<String>();
        userWords.add("word");
        userWords.add("longlongword");
        userWords.add("hello");

        WordSearchPuzzle puzzle = new WordSearchPuzzle(userWords);
        puzzle.createPuzzleGrid();
        puzzle.debugPuzzle();
    }

}

结果

[[w, o, r, d], [l, o, n, g, l, o, n, g, w, o, r, d], [h, e, l, l, o]]
于 2012-04-17T11:27:54.120 回答
0

标记为家庭作业。就像你之前的问题一样,我会问:你尝试过什么了吗?你打算如何解决这个问题?你目前的问题是什么?

我给你一个主意。createPuzzleGrid() 方法为您初始化拼图数组。请注意他们如何从单词列表中计算维度。这应该为您指明正确的方向:如何将字符从单词列表复制到数组中。

于 2012-04-17T11:19:35.800 回答