简体   繁体   English

function 返回一个没有数据的 ArrayList

[英]function returns an ArrayList empty of data

been trying to add an object of a class to an ArralyList of objects when a user clicks a button in javaFx.当用户单击 javaFx 中的按钮时,一直试图将 class 的 object 添加到对象的 ArralyList 中。 I am trying to add an object of a class (Product) to an ArrayList shoppingList, which is an ArrayList of Products.我正在尝试将 class (产品)的 object 添加到 ArrayList 购物清单中,这是 Products 的 Z57A97A39435CFDFED96E03。 when I test the ArrayList in the addToCart() function,everything is fine, but when the program goes back to main the ArrayList is empty of data当我在 addToCart() function 中测试 ArrayList 时,一切都很好,但是当程序返回主程序时,ArrayList 没有数据

here is my main where I call the function这是我主要的地方,我称之为 function

ArrayList<Product> shoppingList = new ArrayList<>();
    Button btnAddToCart = new Button("Add to cart");

  btnAddToCart.setOnAction(e->addToCart(productList,productBox,shoppingList));

here is the function这是 function

 public ArrayList<Product> addToCart(ArrayList<Product> productList, ChoiceBox productBox, ArrayList<Product> shoppingList) {



    for(int x =0 ; x<productList.size();x++)
    {
        if(productList.get(x).getProductDescribtion() == productBox.getValue())
        {
            Product inCartProduct = new Product(productList.get(x).getProductCategory(),productList.get(x).getProductDescribtion(),
                    productList.get(x).getItemNumber(),productList.get(x).getInStockQty(),productList.get(x).getProductPrice() );

            System.out.println("Cart in for loop :>"+inCartProduct.toString());

            shoppingList.add(inCartProduct);


        }
    }
    for(Product onePr : shoppingList)
    {
       // System.out.println("Here in the cart : ");
        System.out.println("Cart in button function :> "+onePr.toString());
      //  System.out.println("The size of the cart : "+shoppingList.size());
    }


    return  shoppingList;
}

For comparing values use .equals(...) instead of ==对于比较值使用.equals(...)而不是==

if(productList.get(x).getProductDescribtion().equals(productBox.getValue()))

... instead of ... 代替

if(productList.get(x).getProductDescribtion() == productBox.getValue())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM