【问题标题】:ArrayList emptying itself?ArrayList 清空自己?
【发布时间】:2013-12-07 17:41:38
【问题描述】:

请遵循 eventList 的路径。虽然它最初应该存储正确的对象,但它会在进入 MouseClicked() 时立即清空自己。有一个驱动程序用于在另一个类中运行 initialize()。我似乎无法让 eventList 保存其信息。

public class Adventure_Chapter1 implements MouseListener
{
boolean success = true;
ArrayList<StoryEvent> eventList = new ArrayList<StoryEvent>();

public void initiliaze() throws FontFormatException, IOException
{
load();     // loads StoryEvents
play(0);
System.out.println("Init() eventList size: " + eventList.size());
}

private void load()
{
    int x = 0;
    switch(x)
    {
    case 0:
    StoryEvent txt0 = new StoryEvent(parameters);
    eventList.add(txt0); 
    case 1:
    StoryEvent assassinStart = new StoryEvent(parameters);
    eventList.add(assassinStart);
}
}

public void updatePlayer()
{
    System.out.println("Player Updated ");
}

public void play(int c)                                 // to be implemented
{
    storyLineDisplay.setText("testing");
    System.out.println("Play() eventList size:" + eventList.size());
    //int c would typically change my buttons' options next to them. but for now it
      is irrelevant.
 }

public void mouseClicked(MouseEvent e)
{
    if (e.getSource().equals(buttonOne));
    {
        if (success == true)
        {
            updatePlayer();
            System.out.println("MouseClicked eventList size: " + eventList.size());
            play(1);
        }
        else
        {
            updatePlayer();
            currentCharacter.add(eventList.get(choice));
            choice = currentCharacter.get(currentCharacter.size() -1).getFail1();
            play(1);
        }
    }

}

输出在这里:

Play() eventList size:2
Init() eventList size: 2
Player Updated 
MouseClicked eventList size: 0
Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index: 1, Size: 0
at java.util.ArrayList.RangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at Adventure_Chapter1.mouseClicked(Adventure_Chapter1.java:277)
     insert further lines of errors here.

【问题讨论】:

  • 请应用正确的格式!
  • 在我们回答这个问题之前,您想帮自己一个忙,并删除对您的问题没有影响的代码,直到您拥有仍然显示问题的最小代码库。当你这样做的时候,你几乎肯定会发现问题。
  • 不知道是不是和问题有关系,但是mouseClicked:if (e.getSource().equals(buttonOne));这一行多了一个分号,需要去掉。
  • 我建议您在调试模式下运行您的代码并在eventList 上添加一个监视。您提供的代码对我来说看起来不错。可能您在其他地方覆盖了您的列表。
  • 编辑删除更多代码。现在的任何内容都会影响 eventList 或需要跟随输出。

标签: java arraylist awt mouselistener


【解决方案1】:
  1. 您应该中断您的 switch 语句。
  2. if (success == true)

    可以替换为

    if (success)
    
  3. if (e.getSource().equals(buttonOne));

    必须替换为

    if (e.getSource().equals(buttonOne))
    
  4. 如果您需要调试帮助,请将所有涉及的代码放入其中,我们不是读心者和猜测者。显然是缺少代码。

已编辑

我们可以看看 StoryEvent 的代码吗?你在触摸那里的evenList 列表吗 ?否则我不明白你是如何在你的 evenList,开始。

【讨论】:

    【解决方案2】:
    public void mouseClicked(MouseEvent e)
    {
    if (e.getSource().equals(buttonOne));
    {
        if (success == true)
        {
            updatePlayer();
            System.out.println("MouseClicked eventList size: " + eventList.size());
            play(choice);
        }
        else
        {
            updatePlayer();
            currentCharacter.add(eventList.get(choice));
            choice = currentCharacter.get(currentCharacter.size() -1).getFail1();
    
        }
    }
    }
    

    但什么是“选择”?

    【讨论】:

    • 目前的选择是 1,我很抱歉。这实际上与我的问题无关。我用它来更改我的 GUI 中的 JTextAreas。代码中定义的,只是被删掉了。
    猜你喜欢
    • 1970-01-01
    • 2017-10-07
    • 1970-01-01
    • 2020-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多