2017-05-29 65 views
-1

我剛剛開始了一個戰爭紙牌遊戲。我製作了一個套牌,然後需要重新洗牌。無論如何,我將需要.length以後纔是正確的。所以當我用這個長度的時候,當我計算的時候它應該是52,所以它得到了160的答案。這是代碼。Java Array.Length不正確

import java.util.List; 

public class wargame { 
    public static void main(String args[]){ 
     funtionsforwargame funtionsforwargame=new funtionsforwargame(); 
     funtionsforwargame.instructions(); 
     List<String> randomized_deck=funtionsforwargame.get_deck(); 
     String[] deck_convert = randomized_deck.toArray(new String[0]); 
     String deck=java.util.Arrays.toString(deck_convert); 
     System.out.println(deck); 
     System.out.println(deck.length()); 

    } 

} 

此外,get_deck方法創建一個甲板,作爲數組,然後轉換爲一個列表字符串,並因爲它需要進行改組返回它。所以我接下來返回列表並將其轉換回數組,然後確保它給我的套牌,而不是像ghty @ 67843那樣的東西。所以如果有人知道它爲什麼給我160而不是52,那會很有幫助。如果你不相信列表/數組中有52個,那麼這裏是get_deck方法。

public class funtionsforwargame { 
    public void instructions(){ 
     System.out.println("Welcome to War!"); 
     System.out.println("The object of the game is to collect all cards from the other person."); 
     System.out.println("Basically, all you do is watch and press any key to go to the next round."); 
     System.out.println("Good luck!!!"); 
    } 
    public List<String> get_deck(){ 
     String new_deck[]={"A","A","A","A","2","2","2","2","3","3","3","3","4","4","4","4","5","5","5","5","6","6","6","6","7","7","7","7","8","8","8","8","9","9","9","9","10","10","10","10","J","J","J","J","Q","Q","Q","Q","K","K","K","K"}; 
     List<String> list_convert = Arrays.asList(new_deck); 
     Collections.shuffle(list_convert); 
     return list_convert; 
    } 

} 

在此先感謝。

+2

我沒有看到你得到任何數組的'length'屬性,只是你在包含其他字符的數組的字符串表示形式上調用'length()',也是如此。逗號和空格,因此從52個1或2個字符元素中可以得到一個160個字符的字符串(類似'[A,A,2,10,...]'48個單個字符+4個雙字符+ 51次' '+'['+']'= 48 + 4 * 2 + 51 * 2 + 1 + 1 = 160個字符) – Thomas

+2

請遵循Java命名約定(大小寫,拼寫),因爲這樣會使您的代碼可讀。例如,很難區分拼寫錯誤的'funtionsforwargame'類和拼寫錯誤的'funtionsforwargame'變量。 –

回答

3

你知道:

int[] x = {1,2,3}; 

String y = Arrays.toString(x); 

是完全不同的東西?

當你做

System.out.println(deck); 
System.out.println(deck.length()); 

長度計數的逗號,空格和[],這些爲什麼會是一樣的52張牌你在randomized_deck有

你不斯內德使用在所有的陣列中,yyour情況下,列表比正常更多...

只是做

List<String> randomized_deck = funtionsforwargame.get_deck(); 

System.out.println(randomized_deck); 

,並分割你可以使用List#subList(....);

+0

我知道一個是數組,另一個是列表。爲了洗牌,我必須進行轉換。如何獲得52的任何答案都會有所幫助。我仍然不知道如何得到逗號,空格和[]。 –

+0

我需要能夠稍後拆分隨機卡組,稍後訪問列表中的每個元素。這意味着長度必須是正確的。 –

+0

我仍然無法獲得randomized_deck的長度,因爲它是列表。 –

2

當你這樣做:

String deck=java.util.Arrays.toString(deck_convert); 

數量52成爲160因爲卡102數字。您應該打印String[] deck_convert而不是使用Arrays.toString(deck_convert)

public class wargame { 
    public static void main(String args[]) { 
     funtionsforwargame funtionsforwargame = new funtionsforwargame(); 
     funtionsforwargame.instructions(); 
     List<String> randomized_deck = funtionsforwargame.get_deck(); 
     String[] deck_convert = randomized_deck.toArray(new String[0]); 
     for(String card : deck_convert) 
      System.out.print(card + ","); 
     System.out.println(deck_convert.length); 
    } 
} 
1

這是真實的,你可以在這個地方檢查:

public class wargame { 

    public static void main(String args[]) { 
     funtionsforwargame funtionsforwargame = new funtionsforwargame(); 
     funtionsforwargame.instructions(); 
     List<String> randomized_deck = funtionsforwargame.get_deck(); 
     String[] deck_convert = randomized_deck.toArray(new String[0]); 
     String deck = java.util.Arrays.toString(deck_convert); 
     System.out.println(deck_convert.length); 
     System.out.println(deck); 
     System.out.println(deck.length()); 

    } 

} 

OUTPUT:

52 
[10, 2, 8, 3, A, 7, 2, 6, 8, 6, 4, K, 3, 9, 10, 3, 5, J, Q, 7, 5, 8, K, J, J, 3, A, 7, 7, 9, 2, 6, K, 4, Q, 9, A, Q, 10, A, 9, 5, K, Q, 5, 6, 8, 10, J, 4, 4, 2] 
160 

在第一種情況下length給你的元素的數量陣列。在另一箇中,您將擁有已轉換字符串的實際長度。 我們可以通過簡單的檢查:

public static void main(String args[]) { 
    funtionsforwargame funtionsforwargame = new funtionsforwargame(); 
    funtionsforwargame.instructions(); 
    List<String> randomized_deck = funtionsforwargame.get_deck(); 
    String[] deck_convert = randomized_deck.toArray(new String[0]); 
    String deck = java.util.Arrays.toString(deck_convert); 
    System.out.println(deck_convert.length); 
    System.out.println(deck); 
    System.out.println(deck.charAt(4)); // here we take one symbol 
    System.out.println(deck.length()); 

} 

OUTPUT:

52 
[A, 7, 2, A, 2, 2, 5, A, 3, 8, 6, Q, 10, 4, K, 4, J, 3, 5, 7, 8, Q, 7, 5, 10, K, 10, 4, 9, 6, Q, 8, K, 7, 6, 9, J, J, 10, K, 5, 2, 8, 6, J, Q, 3, 4, 9, A, 3, 9] 
7 
160 

正如我們所看到的命令System.out.println(deck.charAt(4));給我們**7**

+0

那麼爲了稍後訪問每個元素,我將不得不從甲板上訪問,但對於第一個元素來說,它將是第一個而不是第0個元素,因爲[? –

+0

是的,我只是改進了這個例子 –