【问题标题】:Displaying an array list item by item in GUI在 GUI 中逐项显示数组列表
【发布时间】:2013-11-27 03:54:41
【问题描述】:

我正在尝试逐项显示具有多种类型的数组列表。所以我想这是一个两部分的问题。首先,如何将此数组列表转换为字符串以将其打印出来(我不确定这有多重要)。其次,我如何使用 ActionEvent 从一个项目移动到另一个项目。

我的数组列表有 String、int、int、double。我已经想出了如何将双精度格式化为字符串,但是如何为整个数组列表执行此操作? arraylist 已经排序,以便按字母顺序显示。

下面是我的数组列表和排序

inventory.add(new inventoryItem("Pencil", 1111, 50, .25));
    inventory.add(new inventoryItem("Pen", 2222, 50, 1.00));
    inventory.add(new inventoryItem("Marker", 3333, 5, 2.00));
    inventory.add(new inventoryItem("Notebook", 4444, 10, 2.50));
    inventory.add(new officeSupplyItem("Mechanical Pencil", 1112, 25, .50));
    inventory.add(new officeSupplyItem("Lead Pencil", 1113, 25, .25));
    inventory.add(new officeSupplyItem("Blue Pen", 2221, 25, 1.00));
    inventory.add(new officeSupplyItem("Black Pen", 2223, 5, 1.00));
    inventory.add(new officeSupplyItem("Red Pen", 2224, 20, 1.00));
    inventory.add(new officeSupplyItem("Steno Notebook", 4441, 5, 2.50));
    inventory.add(new officeSupplyItem("Legal Pad", 4442, 5, 2.50));

    inventory = sortInventory(inventory);
    for (int i = 0; i < inventory.size(); i++) {
        inventory.get(i).output(outputText);
    }
    inventoryTotal(inventory, outputText);
    sortInventory(inventory);
}

static ArrayList sortInventory(ArrayList<inventoryItem> unsorted) {
    ArrayList<inventoryItem> sorted = new ArrayList<>(); //create new array list to sort
    inventoryItem alpha = null;
    int lowestIndex = -1;
    while (unsorted.size() > 0) { //while my unsorted array is less than 0 do the following
        for (int i = 0; i < unsorted.size(); i++) { //increment through 
            if (alpha == null) {
                alpha = unsorted.get(i); //get the next line in the inventoryItem array
                lowestIndex = i;
            } else if (unsorted.get(i).itemName.compareToIgnoreCase(alpha.itemName) < 0) { //compare items to determine which has a higher value
                alpha = unsorted.get(i);
                lowestIndex = i;
            }

        }
        sorted.add(alpha); //reset the index so it will loop until there are no more items in the unsorted array
        unsorted.remove(lowestIndex);
        alpha = null;
        lowestIndex = -1;
    }
    return sorted; //return the sorted arraylist

}

我的按钮是这样设置的

 nextButton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            outputText.append("\n this should move me forward in the list");

        }
    });
    previousButton.addActionListener(new ActionListener(){

        @Override
        public void actionPerformed(ActionEvent e) {
            outputText.append("\n this should move me backward in the list");

        }
    });

    exitButton.addActionListener(new ActionListener() { //setup listener for the exit button

        @Override
        public void actionPerformed(ActionEvent ae) {
            System.exit(0); //once the exit button is pressed, exit the program

        }

    });
    thePanel.add(nextButton); //move forward in the arraylist
    thePanel.add(previousButton); //move to previous item in the arraylist
    thePanel.add(exitButton); //add exit button to the panel

任何帮助将不胜感激。

【问题讨论】:

  • 你有什么问题?
  • 真的是两部分。我认为我必须使用列表迭代器才能在前后两个方向上逐项移动我的arraylist,这样做应该是正确的字符串吗?所以我的第一个问题是如何将我的数组列表转换为每行一个字符串?第二个问题是如何使用列表迭代器和 gui 按钮在列表中移动?
  • "将我的数组列表转换为一个字符串" 遍历它并在每个元素上调用toString()

标签: java user-interface arraylist append


【解决方案1】:

Object#toString() 应该在 InventoryItem 中被覆盖以获得您想要的显示结果。示例:

public class InventoryItem {
    String type;
    int id;
    int quantity;
    double cost;

    public InventoryItem(String type, int id, int quantity, double cost){
        this.type = type;
        this.id = id;
        this.quantity = quantity;
        this.cost = cost;
    }

    // override toString
    @Override
    public void toString(){
        return "Type: " + type
               + ", Id: " + id
               + ", Qty: " + quantity
               + ", Cost: " + cost;
    }
}

当您打印 InventoryItem 时,它看起来像这样

输出:Type: pencil, Id: 1111, Qty: 50, Cost: .25

要从一个索引移动到另一个索引,您可以保留一个currentIndex 变量

int currentIndex = 0;

ArrayList<InventoryItem> inventory = new ArraList<>();

previousButton.addActionListener(new ActionListener(){

    @Override
    public void actionPerformed(ActionEvent e) {
        if (currentIndex > 0) {
            currentIndex--;

            outputText.append(inventory.get(currentIndex) + "\n");
        }
    }
});

注意:考虑 Java 命名约定。类名应以大写字母开头

【讨论】:

  • 这似乎工作得很好,除了不能让 currentIndex 工作。我要么遇到从静态上下文调用它的问题,要么找不到变量。它不应该进入主课吗?
  • 将其放置在您声明 ArrayList&lt;InventoryItem&gt; inventory = new ArraList&lt;&gt;(); 的相同位置。我不完全是您的代码的外观,但如果第一个不起作用,请尝试将currentIndex 设为静态。尽管如果不看到您的代码是结构化的就很难分辨。如果您尝试从static 方法调用currentIndex,它也应该是静态的。
  • currentIndex 应该在全局范围内,如果这有帮助的话。
  • 我已经在我的公共类测试中声明了它,这在我的主要之前 - 如果这有帮助的话。我宣布它是私有的,但我不确定这是否正确。
  • 什么不起作用?您的代码中可能还有其他问题。一方面,我在您的 sortInventory() 方法中看到,您正在创建一个 new Arrayist 而不是修改原始文件。然后您尝试以静态方式调用它,(不为其分配变量并使用该变量)sortInventory(inventory);。可能有多个错误。这可能不是您根据我的建议所做的修改。
【解决方案2】:
  1. 要显示对象的属性,您需要实现 inventoryItem 对象的“toString()”方法(请注意,在 JAVA 类名称中应以大写字母开头)
  2. 在 toString 方法中,您可以创建一个您希望显示的字符串,就像您甚至可以连接属性名称一样。例如,您可以像这样显示它 商品名称:钢笔,商品编号:1111 ..
  3. 由于需要前后方向遍历,所以应该考虑使用链表或者双向链表集合。

【讨论】:

    猜你喜欢
    • 2017-06-19
    • 2020-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多