2016-05-22 79 views
1

我有一個數組字符串,我想將第一個元素存儲在一個字符串中,並刪除數組中的第一個元素,這裏是我到目前爲止所擁有的。從字符串數組中刪除第一個字符串android studio

是導致我的錯誤代碼是:

public String giveCard(){ 
    shuffle_cards(); 
    String random = deck_list.remove(0); 
    Log.i("random", random); 
    return random; 
} 

這裏是整個頁面

import android.app.Activity; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.util.Log; 

import java.sql.Array; 
import java.util.ArrayList; 
import java.util.Arrays; 
import java.util.Collections; 
import java.util.List; 
import java.util.Random; 

public class MainActivity extends Activity { 

    List<String> deck_list; 
    String[] users_cards, computers_cards; 
    String[] deck = { 
      "ace-clubs", "ace-diamonds", "ace-hearts", "ace-spades", 
      "two-clubs", "two-diamonds", "two-hearts", "two-spades", 
      "three-clubs", "three-diamonds", "three-hearts", "three-spades", 
      "four-clubs", "four-diamonds", "four-hearts", "four-spades", 
      "five-clubs", "five-diamonds", "five-hearts", "five-spades", 
      "six-clubs", "six-diamonds", "six-hearts", "six-spades", 
      "seven-clubs", "seven-diamonds", "seven-hearts", "seven-spades", 
      "eight-clubs", "eight-diamonds", "eight-hearts", "eight-spades", 
      "nine-clubs", "nine-diamonds", "nine-hearts", "nine-spades", 
      "ten-clubs", "ten-diamonds", "ten-hearts", "ten-spades", 
      "jack-clubs", "jack-diamonds", "jack-hearts", "jack-spades", 
      "queen-clubs", "queen-diamonds", "queen-hearts", "queen-spades", 
      "king-clubs", "king-diamonds", "king-hearts", "king-spades", 
      "joker-one", "joker-two" 
    }; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     shuffle_cards(); 
     users_cards[0] = giveCard(); 
     users_cards[1] = giveCard(); 
     users_cards[2] = giveCard(); 
     users_cards[3] = giveCard(); 
     users_cards[4] = giveCard(); 

     computers_cards[0] = giveCard(); 
     computers_cards[1] = giveCard(); 
     computers_cards[2] = giveCard(); 
     computers_cards[3] = giveCard(); 
     computers_cards[4] = giveCard(); 

    } 

    public void shuffle_cards(){ 

     deck_list = (Arrays.asList(deck)); 
     Collections.shuffle(deck_list); 
     String[] shuffle_deck = deck_list.toArray(new String[deck_list.size()]); 
     Log.d("Shuffled Deck", "arr: " + Arrays.toString(shuffle_deck)); 


    } 

    public String giveCard(){ 
     shuffle_cards(); 
     String random = deck_list.remove(0); 
     Log.i("random", random); 
     return random; 
    } 


} 
+0

沒有意義在這裏使用數組。使用堆棧,隊列或列表(您需要自己刪除項目)。 – Tom

回答

0
deck_list = (Arrays.asList(deck)); 
... 
String random = deck_list.remove(0); 

Arrays.asList(deck)返回一個固定大小的List,所以你不能刪除元素它。這就是deck_list.remove(0)引發異常的原因。

從Javadoc中:

<T> List<T> java.util.Arrays.asList(T... a)

返回由指定數組支持的固定大小的列表。 (對返回列表的「寫通」改爲數組)。此方法充當基於數組和基於集合的API之間的橋樑,並結合使用Collection.toArray。返回的列表是可序列化的並實現RandomAccess。

您可以使用

deck_list = new ArrayList<String>(Arrays.asList(deck)); 

,以創建可變大小的List

此外,必須先實例化users_cardscomputers_cards陣列:

String[] users_cards = new String[5]; 
String[] computers_cards = new String[5]; 
+0

我得到這個錯誤「嘗試寫入空數組」for users_cards [0] = giveCard(); –

+0

@YudiMoszkowski你永遠不會初始化'users_cards',所以它不是一個驚喜它是空的。 – Eran