2013-02-11 86 views
-2

我可以轉換一個數組,像這樣:轉換字符串數組在Java中

String [] [] friend = new String [] [] {{{robert, marie}, {jhony, eva}, ​​{mike, laura}, ​​{adrian, rachel}, {kristen, anthony}}; 

寧缺勿濫兩對夫婦,結果是這樣的:

String [] [] friend = new String [] [] {{{robert, marie}, {adrian, rachel}}; 

以後做出這樣:

String [] friendfin = new String [] {{robert, marie, adrian, rachel}}; 

任何人都可以幫我用這個代碼請...謝謝

+3

您是否有問題嗎? – 2013-02-11 20:53:31

回答

0

以下是單程:

String[][] friends = new String[][]{ 
    {robert, marie}, {jhony, eva}, ​​{mike, laura}, 
    ​​{adrian, rachel}, {kristen, anthony} 
}; 
List<String> list = new ArrayList<String>(); 
for(String[] array : friends) 
{ 
    for(String friend : array) list.add(friend); 
} 
Collections.shuffle(list); 
String[] friendfin = new String[list.length]; 
list.toArray(friendfin); // fill the array 
friendfin = Arrays.copyOf(friendfin, 4); 
+1

@Aubin'list.addAll()'把'Collection'不是一個數組! – 2013-02-11 20:59:11