1
String [] words = { "apple", "orange", };
String Word = words[wordGenerator];            //wordsGenerator is just a rand(); which return a integer.

String [] letters;
List<String> wordList;

letters = new String[12];
wordList = new ArrayList<String>(Arrays.asList(letters));

letters = Word.split("(?!^)");

通过执行上面的代码,我知道如果它生成苹果,我将得到一个名为“字母”的数组,前 5 个内存为 a、p、p、l、e,对吗?

如果是这样,我如何将这 5 个字符串从“字母”数组传递到 wordList?解决方案似乎很简单,但我就是想不出来。任何帮助将不胜感激。谢谢你。

4

2 回答 2

1
 for(int i=0;i<letters.length();i++{
 wordlist.add(letters[i]);
 }

我想..这就是你要问的......?

于 2012-04-24T09:27:49.230 回答
0

这是一个片段代码:

/**
 * @param args
 */
public static void main(String[] args) {
    //init the wordList
    List<String> wordList=new ArrayList<String>();
    //init the word buffer
    String [] letters = new String[12];
    //init the data
    String [] words = { "apple", "orange", };
    //pick a random number
    int wordsGenerator = new Random().nextInt(words.length);
    //pick a random word
    String Word = words[wordsGenerator];            
    //process the word...
    letters = Word.split("(?!^)");
    //add the word to the wordList
    String mergedWord = join(letters);
    wordList.add(mergedWord);

}

/*
 * Merges a set of strings to one string
 */
static String join(String[] strs){
    StringBuffer sb=new StringBuffer();
    for (String item : strs) {
        sb.append(item);
    }
    return sb.toString();
}

}

于 2012-04-24T09:53:01.863 回答