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("(?!^)");
通过上面的代码,我知道我会得到一个名为'letters'的数组,前5个内存为a,p,p,l,e,如果它生成苹果我是对的吗?
如果是这样,我如何将这5个字符串从'letters'数组传递到wordList?解决方案似乎有点容易,但我无法提出它。任何帮助将不胜感激。谢谢。
答案 0 :(得分:1)
for(int i=0;i<letters.length();i++{
wordlist.add(letters[i]);
}
我想..这就是你要问的......?
答案 1 :(得分: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();
}
}