我知道我在这里缺少一些简单的东西。我完成了这项功课,除了通过我的ArrayList。这是我需要从ArrayList中提取和删除对象的计算器的撤消功能。这是方法:

public void actionPerformed(ActionEvent e)
     {
         Status state;
         state = new Status(operand1, operator, operand2, displayBox.getText());
         //ArrayList that I am coping into 
         listOfStates.add(state);
         super.actionPerformed(e);


         if(e.getSource() == undo )
         {
             Status previousState  = (Status) listOfStates.get(listOfStates.size()- 1); 
             displayBox.setText(" ");
              displayBox.setText(displayBox.getText()  +  previousState.op1); 
//This is where I need help at?  This calls a method of Status that only returns op1 IE 
//first operator

          }   
      }

整个班级都在这里

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.*;
/**
 *
 * 
 * 
 */
public class BetterCalculator extends Calculator 

{   
    //attributes
    protected JButton undo; 
    protected ArrayList<Status> listOfStates; 
    private int numClicks = 0;





    public BetterCalculator()
    {
        super();
        listOfStates = new ArrayList<Status>();

    }


    public void createUserInterface3()
    {
        createUserInterface2();
        undo = new JButton("undo");
        jPanel.add(undo);
        undo.setBackground(Color.red);
       undo.setToolTipText("This is the undo feature");
       undo.addActionListener(this);

    }


     public void actionPerformed(ActionEvent e)
     {
         Status state;
         state = new Status(operand1, operator, operand2, displayBox.getText());
         //ArrayList that I am coping into 
         listOfStates.add(state);
         super.actionPerformed(e);


         if(e.getSource() == undo )
         {
             Status previousState  = (Status) listOfStates.get(listOfStates.size()- 1); 
             displayBox.setText(" ");
              displayBox.setText(displayBox.getText()  +  previousState.op1);

          }   
      }


    public static void main(String[] args)
    {

        BetterCalculator myCalc;
        myCalc = new BetterCalculator();
        myCalc.createUserInterface3();



    }
}

状态类

import java.util.*;
import java.awt.event.*;
import java.awt.*;
/**
 * Write a description of class Status here.
 * 
 * 
 *  This is a class to get the status for the undo feature
 */
public class Status 
{   
    //attributes
   protected double op1;
   protected char opt;
   protected double op2;
   protected String soFar;
    //constructors



    public Status(double o1, char op, double o2, String sf)
    {
        op1 = o1;
        opt = op;
        op2 = o2;
        soFar = sf;
    }



  //Methods
   public double getOp1()
    {
        return op1;
    }


     public char getOpt()
    {
        return opt;
    }


    public double getOp2()
    {
        return op2;
    }






}

感谢您的帮助。我知道我错过了如何将对象拉出ArrayList然后将其删除。

有帮助吗?

解决方案

如果你想要的是能够从集合中取出对象并将其删除,你可能需要考虑使用队列 Deque 取而代之,取决于您想要从哪个目标移除对象。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top