在此代碼中,我嘗試使用堆棧向數組列表中添加新的順序,因爲我不允許使用add(),但是當我嘗試將它稱爲程序顯示一個錯誤,指出了push()方法沒有被發現,這是巨大的,如果有人能告訴我在哪裏出了錯代碼在java中使用堆棧將對象添加到數組列表中
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);
}
請注意,接口'Deque'具有'push'和'pop'方法;它由類LinkedList和ArrayDeque實現。 –