Java ArrayList 给我一个不安全操作警告

标签 java generics

我最近开始从事一个个人项目,以测试我的(糟糕的)java 技能。这是一个基本的 Java 卡游戏,名为“Go Fish!”。我目前仅使用基本的控制台输入/输出来制作游戏。但是每当我编译 MainClass 类时,编译器都会向我发出“未检查或不安全”操作警告。我在互联网上寻找解决方案,并尝试了很多,但它总是给我这个错误。

关于如何摆脱这个问题有什么想法吗?

代码:类MainClass

    import java.util.*;

public class MainClass
{
    //Playing states
    public static final int PLAYING = 0;
    public static final int OVER = 1;

    //Chance states
    public static final int PLAY = 0;
    public static final int COMP = 1;

    //Win conditions.
    public static final int PLAY_WIN = 0;
    public static final int COMP_WIN = 1;

    //Values used in the game, VERY IMPORTANT
    public static int currentState, currentPlayer;
    public static int winner;

    public static ArrayList<String> Player_Cards;
    public static ArrayList<String> Comp_Cards;
    public static ArrayList<String> Deck_Cards;

    public MainClass() {

    }

    public static void main(String[] args) {
        //Start Game. This action is carried out EVERYTIME the game is started.
        initGame();  //Serves function for "Initializing" the game i.e. sorting cards, distributing, identifying.
    }

    public static void initGame() {
        //Goto Shuffler, get a shuffled deck, return here.
        Shuffler shuffle = new Shuffler();
        Deck_Cards = new ArrayList<String>(shuffle.doShuffle()); //For safe usage.

        Player_Cards = new ArrayList<String>();
        Comp_Cards = new ArrayList<String>();

        //Give player cards        
        for(int i = 0; i < 5; i++) {
            int c = Deck_Cards.size() - 1 - i;
            Player_Cards.add(Deck_Cards.get(c));
            Deck_Cards.remove(c);
        }

        //Give computer cards       
        for(int i = 0; i < 5; i++) {
            Comp_Cards.add(Deck_Cards.get(i));
            Deck_Cards.remove(i);
        }      
        System.out.println("Darp");
    }
}

代码:类洗牌器:

import java.util.*;

public class Shuffler
{
    public enum Suits {HEARTS, DIAMONDS, CLUBS, SPADES};
    public enum Ranks {ACE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING};

    public Shuffler() {

    }



    public static ArrayList doShuffle() {
        //Initialize cards
        int count = 0;
        ArrayList<String> deck = new ArrayList<String>();
        for(int i = 0; i < 4; i++) {
            for(int y = 0; y < 13; y++) {
                deck.add("Card is " + Ranks.values()[y] + " of " + Suits.values()[i]);
                count++;
            }
        }
        Collections.shuffle(deck);

        return deck;
    }
}

其他信息:

我在这个项目中使用 BlueJ

最佳答案

您在 doShuffle 方法中使用原始类型,因此编译器会提示缺乏类型安全性。替换

public static ArrayList doShuffle() {

public static List<String> doShuffle() {

使用 Shuffler 实例访问此方法,因此可以省略 static 关键字

关于Java ArrayList 给我一个不安全操作警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23951743/

相关文章:

java - 将泛型应用于 java.util.Function

java - 如何为 Java 类字段生成准确的泛型表达式?

java - 在 Java 中检查字节数组中的各个位?

java - 方法流程控制

java - 访问webhdfs时出现UnsupportedOperationException

c# - 隐式和显式委托(delegate)创建之间的区别(有和没有泛型)

java - NullPointerException setOnClickListner

java - NoClassDefFoundError com/google/api/services/storage/Storage$Builder

c# - "in"通用参数有什么作用?

swift - 有没有一些方法可以检查类是否是从泛型类型继承的?