在这段代码中,由于不允许使用add(),我尝试使用堆栈将新顺序添加到数组列表中,但是当我尝试调用它时,程序显示错误,提示未找到push()方法,如果有人能告诉我代码中哪里出了问题,那是很棒的

import java.util.ArrayList;
import java.util.Scanner;

public class OrderArrayList {
      ArrayList<OrderList> orderList;
      public OrderArrayList() {
           orderList = new ArrayList();
      }

      public boolean isEmpty() {
           return orderList.isEmpty();
      }

       public void push(OrderList x) {
           orderList.add(x);
      }

      public void addOrder() {
           Scanner input1 = new Scanner(System.in);
           System.out.print("Product code: ");
           String pcode = input1.nextLine();

           Scanner input2 = new Scanner(System.in);
           System.out.print("Customer Code: ");
           String ccode = input2.nextLine();

           Scanner input3 = new Scanner(System.in);
           System.out.print("Quantity: ");
           int quantity = input3.nextInt();

           OrderList order = new OrderList(pcode, ccode, quantity);
           orderList.push(order);
       }

最佳答案

难道不是要“隐藏” ArrayList吗?这就是为什么您添加了push()函数的原因?

所以orderList.push(order);应该是push(order);

10-01 14:29