2012-04-24 47 views
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存儲器作爲,P,P,L,E,如果它產生的蘋果我是嗎?增加從一個字符串數組串轉化成一個ArrayList

如果是這樣,我怎樣才能將這5個字符串從'字母'數組傳遞到wordList?這個解決方案似乎很簡單,但我無法解決它。任何幫助將不勝感激。謝謝。

回答

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

我想..這是你在問什麼...?

+0

感謝拉朱。足夠的足夠了。 ;) 我改變添加設置。 for(int i = 0; i Axelgria 2012-04-24 09:42:09

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(); 
} 

}

相關問題