【发布时间】: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