简体   繁体   English

如何逐个将项添加到ArrayList中而不是互相替换?

[英]How do I add items into ArrayList one by one and not replace each other?

I want to create a shopping list application. 我想创建一个购物清单应用程序。 I'm not sure how to add items one by one so the list expands instead of replacing an existing item. 我不确定如何逐个添加项目,因此列表扩展而不是替换现有项目。 This is my code at the moment. 这是我目前的代码。 I was unsuccessfully trying to use for loop. 我尝试使用for循环失败了。

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         

    List<ShoppingItem> list = new ArrayList<>();

    String name = ShoppingListUI.jTextField1.getText();    
    double price = Double.parseDouble(jTextField2.getText());
    int quantity = Integer.parseInt(jTextField3.getText());

    ShoppingItem item = new ShoppingItem(name, price, quantity);
    list.add(item);

    for (ShoppingItem temp : list) {
        System.out.println("Item: " + temp.getName() + ", Price: " + temp.getPrice() + ", Quantity: " + temp.getQuantity());
    }

    jTextField1.setText("");
    jTextField2.setText("");
    jTextField3.setText("");    
}

You are creating a new List within your method : 您正在方法中创建一个新列表:

List<ShoppingItem> list = new ArrayList<>();

so each time jButton1ActionPerformed is called, a new List is created, which contains just one element. 所以每次调用jButton1ActionPerformed ,都会创建一个新的ListjButton1ActionPerformed包含一个元素。

You should change that list to be an instance variable of the class that contains this method. 您应该将该list更改为包含此方法的类的实例变量。

public class YourClass {

    List<ShoppingItem> list = new ArrayList<>();

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        String name = ShoppingListUI.jTextField1.getText();    
        double price = Double.parseDouble(jTextField2.getText());
        int quantity = Integer.parseInt(jTextField3.getText());

        ShoppingItem item = new ShoppingItem(name, price, quantity);
        list.add(item);

        for (ShoppingItem temp : list) {
            System.out.println("Item: " + temp.getName() + ", Price: " + temp.getPrice() + ", Quantity: " + temp.getQuantity());
        }

        jTextField1.setText("");
        jTextField2.setText("");
        jTextField3.setText("");    
    }
}

I'm not sure how to add items one by one so the list expands instead of replacing an existing item. 我不确定如何逐个添加项目,因此列表扩展而不是替换现有项目。

That's because you are creating a new list on each action. 那是因为您正在为每个操作创建一个新列表。 You should move your list declaration to top level. 您应该将列表声明移到顶层。 So that you list stays constant and it keeps adding items in it. 因此,您列出保持不变,并继续在其中添加项目。

List<ShoppingItem> list = new ArrayList<>();

......// other codes 

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         

String name = ShoppingListUI.jTextField1.getText();    
double price = Double.parseDouble(jTextField2.getText());
int quantity = Integer.parseInt(jTextField3.getText());

ShoppingItem item = new ShoppingItem(name, price, quantity);
list.add(item);

for (ShoppingItem temp : list) {
    System.out.println("Item: " + temp.getName() + ", Price: " + temp.getPrice() + ", Quantity: " + temp.getQuantity());
}

jTextField1.setText("");
jTextField2.setText("");
jTextField3.setText("");    

} }


And looking your codes further 并进一步查看您的代码

String name = ShoppingListUI.jTextField1.getText();    
double price = Double.parseDouble(jTextField2.getText());
int quantity = Integer.parseInt(jTextField3.getText());

You are parsing the user inputted text right way. 您正在解析用户输入的文本。 You might want to check for parsing errors and ask user to reenter. 您可能想要检查解析错误并要求用户重新输入。

You always create a new list, when the Action is performed. 执行操作时,始终创建新列表。 The "old" list istn't referenced anymore after that. 之后不再引用“旧”列表了。 You add the new Item to the new List. 您将新项目添加到新列表。 So this is the only item you see. 所以这是你看到的唯一项目。 You need to instantiate the List outside of your Action actionPerformed method. 您需要在Action actionPerformed方法之外实例化List。

private List<ShoppingItem> list = new ArrayList<>();

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt){                                          

    String name = ShoppingListUI.jTextField1.getText();    
    double price = Double.parseDouble(jTextField2.getText());
    int quantity = Integer.parseInt(jTextField3.getText());

    ShoppingItem item = new ShoppingItem(name, price, quantity);
    list.add(item);

    for (ShoppingItem temp : list) {
        System.out.println("Item: " + temp.getName() + ", Price: " + temp.getPrice() + ", Quantity: " + temp.getQuantity());
    }

    jTextField1.setText("");
    jTextField2.setText("");
    jTextField3.setText("");    
}

Currently list in created every call to your method, You need to move it outside the function. 目前列表中创建了对方法的每次调用,您需要将其移出函数。

You need for make it Instance variables, Notice in case you create new class instance every time when calling the function - define list in class as static field and use it. 你需要使它成为实例变量,注意以防每次调用函数时创建新的类实例 - 在类中定义list作为静态字段并使用它。

public static List<ShoppingItem> list = new ArrayList<>();

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

相关问题 我如何一个接一个地将元素添加到ArrayList中? - How do i add elements to my ArrayList one after the other? 当 ArrayList 和构造函数/getter 存储另外两个类时,如何从一个类添加到 ArrayList - How do I add to an ArrayList from one class, when the ArrayList and constructors/getters are stored 2 other classes 如何修改一个 arraylist 而不改变另一个? - How do i modify one arraylist without changing the other? 如何依次访问arrayList中的所有元素? - How do I access all of the elements in an arrayList one after the other? 如何对一个ArrayList使用add()会干扰另一个ArrayList的大小? - How does using add() to one ArrayList interfere the size of the other one? java-如何为每个子类制作一个ArrayList? - java - how do you make one ArrayList for each subclass? 我如何将数组列表的一个元素打印为大写,而其他元素则为小写 - How do i print one element of an arraylist to uppurcase while the other elements lowercase 将arraylist中的新对象添加到另一个arraylist中,而第二个则没有这些新对象 - Add new objects in arraylist to the other arraylist while the second one do not have these new objects 我怎样才能在arraylist的一个单元格中分离项目? - how could I separate items in one cell of an arraylist? 如何将通用项添加到通用ArrayList? - How do I add generic items to a generic ArrayList?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM