문제

나는 여기서 간단한 것을 놓치고 있다는 것을 알고 있습니다. 나는 배열 목록을 통해 움직이는 것을 제외 하고이 숙제를 모두 마쳤습니다. 이것은 계산기의 실행 취소 기능입니다. 계산기에서 배열리스트에서 객체를 가져 와서 제거해야합니다. 방법은 다음과 같습니다.

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;
    }






}

도움을 주셔서 감사합니다. 배열 목록에서 물체를 꺼내고 제거하는 방법에 대해서는 누락된다는 것을 알고 있습니다.

도움이 되었습니까?

해결책

원하는 것은 컬렉션에서 객체를 빼고 제거 할 수있는 기능이라면 사용하는 것을 고려할 수 있습니다. 대기줄 또는 Deque 대신, 객체를 제거하려는 끝의 필요에 따라 의존하십시오.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top