How can I add items to 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 at a time so that the list expands instead of replacing the existing item. This is my code at the moment. I tried unsuccessfully to use for a loop.

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("");    
}

      

+3


source to share


4 answers


A new list is created in your method:

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

      

so every time it is called jButton1ActionPerformed

a new List

one is created containing only one element.



You must change what List

will be the instance variable of the class that contains this method.

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("");    
    }
}

      

+7


source


I'm not sure how to add items one at a time so that the list expands instead of replacing the existing item.

This is because you are creating a new list for each action. You must move your listing ad to the top level. To keep the list constant and it keeps adding items to 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 at your codes further

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

      

You correctly parse the text entered by the user. You might want to check for parsing errors and ask the user to re-login.

+3


source


You always create a new list when the action is performed. After that, the "old" list is no longer mentioned. You are adding a new item to a new list. So this is the only item that you see. You need to instantiate List outside of your ActionPerformed.

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("");    
}

      

+3


source


Currently a list is created every call to your method, you need to move it outside the function.

You need to make it instance variables. Note that when creating a new instance of the class every time you call the function - define list

in the class as a static field and use it.

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

      

+1


source







All Articles