私は配列文字列を持っています。最初の要素を文字列に格納して配列の最初の項目を削除したいと思います。文字列配列から最初の文字列を削除する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;
}
}
ここは、アレイを使用しても意味がありません。スタック、キュー、またはリスト(あなたが自分で項目を削除する必要がある)を使用してください。 – Tom