【问题标题】:ArrayList output repeating itself?ArrayList 输出重复本身?
【发布时间】:2017-11-12 18:04:03
【问题描述】:

所以我尝试使用组合框来显示不同类型食物的不同类别选项。每种食物都有自己的数组列表,例如蔬菜、水果、乳制品等。因此,当用户选择食物类别时,它将在文本区域中显示该类别中的所有食物。但是,只要选择了不同的类别,该列表就会不断重复。所以第一次点击没问题,只会显示一次食物列表。但是,如果您在此之后选择不同的类别,该列表将自行重复。这是我现在使用的代码:

public class foodTypesJFrame extends javax.swing.JFrame {

// Food category arraylists
ArrayList <String> fruitsList = new ArrayList();
ArrayList <String> veggiesList = new ArrayList();
ArrayList <String> dairyList = new ArrayList();



public void foodCategory(String box, String category, ArrayList list)
{
    box = foodCategoryBox.getSelectedItem() + "";

    if (box.equals(category))
    {
        foodOutput.setText("");
        int indexNumber = 0; 
        // Display the different foods
        for (int index = 0; index < list.size(); index++) 
        { 
            indexNumber = index + 1; 
            foodOutput.append(indexNumber + ". " + list.get(index) + "\n"); 
        }
    }
}        

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

    String foodType = foodCategoryBox.getSelectedItem() + "";

    String fruits = "Fruits";
    String veggies = "Vegetables";
    String dairy = "Dairy";

    Collections.addAll(fruitsList, "Apple", "Orange", "Strawberry");
    Collections.addAll(veggiesList, "Lettuce", "Carrot", "Broccoli");
    Collections.addAll(dairyList, "Milk", "Cream", "Cheese");

    foodCategory(foodType, fruits, fruitsList);
    foodCategory(foodType, veggies, vegetableList);
    foodCategory(foodType, dairy, dairyList);

因此,如果我先选择水果类别,它将输出 ” 1. 苹果 2. 橙色 3.草莓” 但是如果我在那之后选择蔬菜类别,它会输出 " 1. 生菜 2.胡萝卜 3.西兰花 4.生菜 5.胡萝卜 6 西兰花”

有没有人对如何制作它有任何建议,以便列表只显示一次?

【问题讨论】:

  • 每次categoryBoxActionPerformed 再次将项目添加到列表中,因此您调用categoryBoxActionPerformed 的次数越多,列表重复的次数就越多
  • 您需要做的就是在每次调用categoryBoxActionPerformed时重新初始化ArrayList

标签: java swing arraylist jcombobox


【解决方案1】:

您在ActionEvent 监听器中调用Collections.addAll(...),这意味着每次点击都会将新元素添加到ArrayList。要解决这个问题,您需要初始化类的构造函数中的所有列表(如果您有固定的产品列表,则更正确),或者在每个列表的 Collections.addAll(...) 之前调用 clear() 方法(如果您真的需要更改事件侦听器内的产品列表)

【讨论】:

    【解决方案2】:

    每次调用categoryBoxActionPerformed 时,都会将相同 项添加到ArrayLists

    要解决此问题,您可以执行以下操作之一:

    1. 每次调用categoryBoxActionPerformed()时,要么重新初始化ArrayLists,所以它看起来像这样:

       //at the beginning of the method.
       fruitsList = new ArrayList<>();
       veggiesList = new ArrayList<>();
       dairyList = new ArrayList<>();
      
    2. 或者用 boolean 包裹它:

       boolean firstTime = true; // create a global variable
      

    然后包装添加:

        if(firstTime){
           Collections.addAll(fruitsList, "Apple", "Orange", "Strawberry");
           Collections.addAll(veggiesList, "Lettuce", "Carrot", "Broccoli");
           Collections.addAll(dairyList, "Milk", "Cream", "Cheese");
           firstTime = false;
        }
    

    【讨论】:

      猜你喜欢
      • 2015-12-29
      • 2017-04-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-07
      • 2013-08-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多