ArrayList清空自己?

时间:2013-11-22 18:12:44

标签: java arraylist awt mouselistener

请遵循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.

2 个答案:

答案 0 :(得分:0)

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 :(得分:0)

  1. 你应该破坏你的开关语句。
  2. if (success == true)

    可以替换为

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

    必须替换为

    if (e.getSource().equals(buttonOne))
    
  4. 如果您需要调试帮助,请填写所有涉及的代码,我们不介意读者和猜测者。显然缺少代码。
  5. <强>被修改

    我们能看到StoryEvent的代码吗?你在那里触摸evenList列表吗?         ?否则我不明白你是如何得到两个元素的         evenList,开头。