【问题标题】:Remove first string from string array android studio从字符串数组android studio中删除第一个字符串
【发布时间】:2026-01-09 10:20:02
【问题描述】:

我有一个数组字符串,我想将第一个元素存储在一个字符串中并删除数组中的第一项,这是我目前所拥有的。

导致我的错误的代码是:

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


}

【问题讨论】:

  • 这里使用数组是没有意义的。使用堆栈、队列或列表(您需要自己删除项目)。

标签: java android arrays list


【解决方案1】:
deck_list = (Arrays.asList(deck));
...
String random = deck_list.remove(0);

Arrays.asList(deck) 返回一个固定大小的List,因此您不能从中删除元素。这就是deck_list.remove(0) 抛出异常的原因。

来自 Javadoc:

&lt;T&gt; List&lt;T&gt; java.util.Arrays.asList(T... a)

返回一个由指定数组支持的固定大小的列表。 (更改返回的列表“直写”到数组。)此方法与 Collection.toArray 结合,充当基于数组的 API 和基于集合的 API 之间的桥梁。返回的列表是可序列化的并实现了 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];

【讨论】:

  • users_cards[0] = giveCard(); 收到此错误“尝试写入空数组”
  • @YudiMoszkowski 你从不初始化users_cards,所以它为空也就不足为奇了。