java.lang.IndexOutOfBoundsException:索引:1,大小:1错误但不知道原因

时间:2015-07-25 08:01:06

标签: java

编辑:有些人建议我在这个帖子中提供更多信息:https://stackoverflow.com/questions/31618657/error-when-adding-int-to-arraylist?noredirect=1#comment51187436_31618657,所以这里有!

所以基本上我正在为学校写一个骰子计划。您应该能够选择正在玩的人数和应该使用的骰子数。然后应该将这些信息记录到TextArea中,这是我设法做到的,并且效果很好!对我来说,事情变得棘手;在程序中还应该有一个TextArea,每轮的高分记录,每轮的获胜者应打印在标签上。我以为我已经解决了这些问题,但是当我尝试运行该程序时,我得到一个错误并且高分并没有赢得胜利者。这是我的代码,我带你完成它!

import java.util.Random;
import java.util.ArrayList;

private void btnPlayActionPerformed(java.awt.event.ActionEvent evt) {                                     
    int numberofdices = Integer.parseInt(comboboxnumberOfDices.getSelectedItem().toString());
    int numberofplayers = Integer.parseInt(comboboxnumberOfPlayers.getSelectedItem().toString());
    int randomNumber;
    int playerHighscore = 0;
    int intWinner = 0;
    String strWinner = " ";

    int P1Points;
    int P2Points;
    int P3Points;
    int P4Points;

    int[] numberOfPlayers = new int[numberofplayers];
    int[] numberOfDices = new int[numberofdices];
    ArrayList<Integer> Points = new ArrayList<>();
    int intPlayerPoints = 0;

    Random randomGenerator = new Random();

    lblWinner.setText(” ”);
    txtaResult.setText("");
    txtaResult.append(”The Dice has been thrown\n\n");

    for (int n = 1; n <= numberOfPlayers.length; n++) {
        txtaResult.append(”Player " + n + ":\n");
        for (int m = 1; m <= numberOfDices.length; m++) {
            randomNumber = randomGenerator.nextInt(5)+1;
            txtaResult.append(”Dice " + m + ” gets value " + randomNumber + "\n");
            intPlayerPoints = intPlayerPoints + randomNumber;
        }
        txtaResult.append("Total score: " + intPlayerPoints + "\n\n”);

    //Here’s where things starts to go wrong...
        Integer playerPoints = new Integer(intPlayerPoints);
        Points.add(playerPoints);
        intPlayerPoints = 0;
    }
    playerHighscore = Points.get(0);

    P1Points = Points.get(0);
    P2Points = Points.get(1);
    P3Points = Points.get(2);
    P4Points = Points.get(3);

    for (int i = 0; i < Points.size(); i++) {
        if (Points.get(i) > playerHighscore) {
            playerHighscore = Points.get(i);
        } 
    }
    if (playerHighscore == P1Points) {
        strWinner = ”Player 1 won!";
        intWinner = playerHighscore;
    } else if (playerHighscore == P2Points) {
        strWinner = ”Player 2 won!";
        intWinner = playerHighscore;
    } else if (playerHighscore == P2Points) {
        strWinner = ”Player 3 won!";
        intWinner = playerHighscore;
    } else if (playerHighscore == P4Points) {
        strWinner = ”Player 4 won!";
        intWinner = playerHighscore;
    }
    lblWinner.setText(" " + strWinner);
    txtaHighscore.append(" " + intWinner + "\n");
}  

好吧,所以第一部分,直到“这里的事情开始出错......” - 评论是变量声明和我想要的代码。在for循环中我首先遍历数量玩耍的人,然后玩骰子的数量。在一个人“转弯”期间玩骰子的价值加在intPlayerPoints - 变量中,然后想法,当第二个for循环是完成,将此值存储在Points - ArrayList中。 (然后为每个人演奏同样的事情)。然后将intPlayerPoints的值设置为零。

然后我将ArrayList的不同值存储在不同的变量中以知道哪个值对应于哪个播放器,然后我使用下面的for循环找出最高值并将其存储在playerHighscore中。最后,我使用if语句来决定playerHighscore - 变量属于哪个玩家,然后打印出来。

现在,问题是,当我按下“播放”按钮时,“这里的所有内容都会出现错误评论...”下面没有任何内容被打印出来(或者,最后两行没有打印出来)程序的其余部分按预期工作,我收到此错误:

Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
    at java.util.ArrayList.rangeCheck(ArrayList.java:635)
    at java.util.ArrayList.get(ArrayList.java:411)
    at mainFrame.MainFrame.btnPlayActionPerformed(MainFrame.java:435)
    at mainFrame.MainFrame.access$000(MainFrame.java:14)
    at mainFrame.MainFrame$1.actionPerformed(MainFrame.java:198)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6516)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
    at java.awt.Component.processEvent(Component.java:6281)
    at java.awt.Container.processEvent(Container.java:2229)
    at java.awt.Component.dispatchEventImpl(Component.java:4872)
    at java.awt.Container.dispatchEventImpl(Container.java:2287)
    at java.awt.Component.dispatchEvent(Component.java:4698)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
    at java.awt.Container.dispatchEventImpl(Container.java:2273)
    at java.awt.Window.dispatchEventImpl(Window.java:2719)
    at java.awt.Component.dispatchEvent(Component.java:4698)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:747)
    at java.awt.EventQueue.access$300(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:706)
    at java.awt.EventQueue$3.run(EventQueue.java:704)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:720)
    at java.awt.EventQueue$4.run(EventQueue.java:718)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:717)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

正如我在编辑这篇文章之前所写的那样,我认为我的代码中的错误是这一行:

Points.add(playerPoints);

因为根据谷歌,与我有同样错误的人有这种问题,但这只是猜测而且可能不是。因此,我非常感谢任何帮助!:)

1 个答案:

答案 0 :(得分:0)

你的堆栈跟踪是说其中一个Points.get调用失败了,因为你试图得到一个元素列表的第二个元素。

弄清楚为什么很难,因为(坦率地说)你的代码是一团糟。但是,我最好的猜测如下:

  1. 此声明将玩家数量设置为1:

    int numberofplayers = Integer.parseInt(
        comboboxnumberOfPlayers.getSelectedItem().toString());
    
  2. 初始化Points的循环正在调用add一次,因此您将获得一个包含一个元素的列表。

  3. 然后你这样做:

    P2Points = Points.get(1);
    

    将抛出一个异常,'因为没有这样的元素。

  4. 因此,导致这些错误的代码有什么问题:

    1. 你得到玩家数量的代码对我来说有点不对劲。使用toString这样可能会很危险,具体取决于组合框的填充方式。无论如何,它给你答案“一个人”......

    2. 这段代码肯定是错的:

      P1Points = Points.get(0);
      P2Points = Points.get(1);
      P3Points = Points.get(2);
      P4Points = Points.get(3);
      

      假设有4个玩家,但实际玩家数量可能会少于......或更多。这是一个BUG。

    3. 更一般地说,我说你的代码是“一团糟” 1 。我的意思是代码中没有计划的迹象。您声明的数组本质上是多余的。如果有4名球员或N名球员,你无法下定决心。

      最后,声明名称以大写字母开头的变量是主要Java样式违规。如果我在标记此代码,您将失去所有样式点。立即

      1 - 这是该词的起源...... https://en.wikipedia.org/wiki/The_Shambles