所以我有一个由用户推测的字符串数组列表。我还有一个二维数组的字符:private char [] [] puzzle;我需要帮助将字符串更改为字符,以便我可以将它们输入到二维数组中,任何人都可以帮忙吗?
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] ;
}
答案 0 :(得分:1)
将每个字符串更改为charArray。
for(String item : ArrayList){
item.toCharArray();
}
答案 1 :(得分:0)
标记为家庭作业。就像你之前的问题一样,我会问:你有没有试过呢?你打算如何解决这个问题?你目前的问题是什么?
我会给你一个想法。 createPuzzleGrid()方法为您填充拼图数组。请注意他们如何从单词列表中计算维度。这应该指向正确的方向:如何将字符列表中的字符复制到数组中。
答案 2 :(得分: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]]