перемещение по Java ArrayList с помощью кнопки отмены

StackOverflow https://stackoverflow.com/questions/1806251

  •  05-07-2019
  •  | 
  •  

Вопрос

Я знаю, что мне здесь не хватает чего-то простого. Я выполнил эту домашнюю работу, за исключением перемещения по моему 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