2017-10-10 46 views
0

在此代碼中,我嘗試使用堆棧向數組列表中添加新的順序,因爲我不允許使用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); 
     } 
+0

請注意,接口'Deque'具有'push'和'pop'方法;它由類LinkedList和ArrayDeque實現。 –

回答

3

不整點「隱藏」 ArrayList?這就是爲什麼你添加了一個push()函數?所以orderList.push(order);應該是push(order);

0
orderList.push(order); 

orderList是ArrayList的引用,而ArrayList類沒有push()方法。 https://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html 您應該創建一個類「OrderArrayList」的實例並調用push()方法。

OrderArrayList orderArrayList = new OrderArrayList(); 
... 
... 
//collect input from user 
OrderList order = new OrderList(pcode, ccode, quantity); 
orderArrayList.push(order);