Closed. This question needs details or clarity。它当前不接受答案。












想改善这个问题吗?添加详细信息并通过editing this post阐明问题。

6年前关闭。



Improve this question





我有一小段代码,设计用来在单击按钮时绘制一张卡片。到目前为止,当我激活actionPerformed()时,出现以下错误(以及其他错误):线程“ AWT-EventQueue-0 java.lang.IndexOutOfBoundsException ...”中的异常。我对此完全陌生,所以如果这是一个简单的错误,请原谅我。

import java.awt.*;
import java.awt.event.*;
import java.util.*; /*Used for Random and ArrayList*/
public class AL extends Frame implements WindowListener,ActionListener {
    TextField text = new TextField(20);
    Button b;
    private int numClicks = 0;
    private static ArrayList<Card> deck=new ArrayList<Card>();
    private static ArrayList<Card> playerOneDeck=new ArrayList<Card>();
    private static ArrayList<Card> playerTwoDeck=new ArrayList<Card>();



    public static void main(String[] args) {
            AL myWindow = new AL("Well of course you know, this means WAR!");
            myWindow.setSize(350,100);
            myWindow.setVisible(true);
            ArrayList<Card> playerTwoDeck=new ArrayList<Card>();
    }//end main()

    public AL(String title) {

            super(title);
            setLayout(new FlowLayout());
            addWindowListener(this);
            b = new Button("Draw");
            add(b);
            add(text);
            b.addActionListener(this);
            ArrayList<Card> deck=new ArrayList<Card>();
            ArrayList<Card> playerOneDeck=new ArrayList<Card>();
            deck=initializeDeck();
            Collections.shuffle(deck);
    }

    public void actionPerformed(ActionEvent e) {
            numClicks++;
            Card theCard=playerOneDeck.get(0);
            text.setText(theCard.name);
            text.setText(theCard.name);
    }

    public void windowClosing(WindowEvent e) {
            dispose();
            System.exit(0);
    }
    /*Assign names, values, and suits to all of the Card objects.*/
    public static ArrayList<Card> initializeDeck() {
        Card[] tempDeck=new Card[52];
        ArrayList<Card> completeDeck=new ArrayList<Card>();
        for (int i=0; i<tempDeck.length; i++) {
            tempDeck[i]=new Card();
            tempDeck[i].name="Two";
            tempDeck[i].value=2;
            i++;
            tempDeck[i]=new Card();
            tempDeck[i].name="Three";
            tempDeck[i].value=3;
            i++;
            tempDeck[i]=new Card();
            tempDeck[i].name="Four";
            tempDeck[i].value=4;
            i++;
            tempDeck[i]=new Card();
            tempDeck[i].name="Five";
            tempDeck[i].value=5;
            i++;
            tempDeck[i]=new Card();
            tempDeck[i].name="Six";
            tempDeck[i].value=6;
            i++;
            tempDeck[i]=new Card();
            tempDeck[i].name="Seven";
            tempDeck[i].value=7;
            i++;
            tempDeck[i]=new Card();
            tempDeck[i].name="Eight";
            tempDeck[i].value=8;
            i++;
            tempDeck[i]=new Card();
            tempDeck[i].name="Nine";
            tempDeck[i].value=9;
            i++;
            tempDeck[i]=new Card();
            tempDeck[i].name="Ten";
            tempDeck[i].value=10;
            i++;
            tempDeck[i]=new Card();
            tempDeck[i].name="Jack";
            tempDeck[i].value=11;
            i++;
            tempDeck[i]=new Card();
            tempDeck[i].name="Queen";
            tempDeck[i].value=12;
            i++;
            tempDeck[i]=new Card();
            tempDeck[i].name="King";
            tempDeck[i].value=13;
            i++;
            tempDeck[i]=new Card();
            tempDeck[i].name="Ace";
            tempDeck[i].value=14;
        }//end FOR
        /*Assign suits*/
        /*keep in mind that the array is now [2], [3],..[k],[a],[2],[3],[k],[a],etc */
        for (int j=0; j<tempDeck.length; j++) {
            tempDeck[j].suit="Hearts";
            j++;
            tempDeck[j].suit="Diamonds";
            j++;
            tempDeck[j].suit="Spades";
            j++;
            tempDeck[j].suit="Clubs";
        }//end FOR

        for (int k=0; k<tempDeck.length;k++) {
            completeDeck.add(tempDeck[k]);
        }
        return completeDeck;
    }//end initializeDeck()

    public void windowOpened(WindowEvent e) {}
    public void windowActivated(WindowEvent e) {}
    public void windowIconified(WindowEvent e) {}
    public void windowDeiconified(WindowEvent e) {}
    public void windowDeactivated(WindowEvent e) {}
    public void windowClosed(WindowEvent e) {}


} //结束AL类

最佳答案

您尝试从PlayerOneDeck的第一行中的ActionPerformed获取第一个元素:Card theCard=playerOneDeck.get(0);但是,PlayerOneDeck是空的ArrayList。它没有元素,因此尝试从中获取元素会返回IndexOutOfBoundsException

您可以同时初始化类变量PlayerOneDeck和构造函数中的同名变量。可能用元素填充了构造函数变量,但是actionPerformed调用了类变量,该变量保持为空。例如:

public class Main {
    ArrayList<Integer> array = new ArrayList<Integer>();  // class variable

    public static void main(String[] args) {
        ArrayList<Integer> array = new ArrayList<Integer>();  // method variable, overrides class variable within the main method
        array.add(1);  // adds 1 to the method variable
        (new Main()).printArray();
    }

    public void printArray() {
        System.out.println(array.get(0));  // tries to print the first element of class variable, throws IndexOutOfBoundsException
    }
}


删除本地变量,仅使用全局类一,您的问题可能会消失。

10-07 23:36