更新 Java 中的 arraylist 方法
Update arraylist method in Java
我正在尝试制作一个类似于此 removeMenu 方法的代码,但我想制作另一种方法来更新我订购的菜单中的产品数量。
餐厅管理员代码:
Menu coffee = new Menu("Coffee", 30);
Menu icedTea = new Menu("Iced Tea", 50);
Menu hotChoco = new Menu("Hot Chocolate", 30);
/*
constructor ommittet.
*/
private ArrayList<Menu> menulist;
public void removeMenu(Menu menumodel) {
for (Menu temp : menulist) {
if (temp.equals(menumodel)) {
menulist.remove(temp);
break;
}
}
}
public void updateMenu(int updateQuant) {
//menulist.set(3, updateQuant);
}
}
}
餐厅class
RestaurantController controller1 = new RestaurantController();
private void DeleteProductActionPerformed(java.awt.event.ActionEvent evt) {
selectedProduct = controller1.getMenulist().get(JTableOrderReceived.getSelectedRow());
if (selectedProduct != null) {
int ans = JOptionPane.showConfirmDialog(this, "The selected product will be removed! ", "DELETE PRODUCT", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
if (ans == JOptionPane.YES_OPTION) {
controller1.removeMenu(selectedProduct);
refTable();
}
}
}
private void UpdateProduct(java.awt.event.ActionEvent evt) {
selectedProduct controller1.getMenulist().get(JTableOrderReceived.getSelectedRow());
if (selectedProduct != null) {
int parseQuant = (int) selectedProduct.getQuantity();
String uQuant = JOptionPane.showInputDialog(this, "Update quantity " + selectedProduct.getItemName() + "\nPrice: " + selectedProduct.getPrice() + "\nCurrent order: " + parseQuant);
int nQuant = Integer.parseInt(uQuant);
controller1.updateMenu(nQuant);
refTable();
}
}
我在餐厅控制器 class 的 updateMenu 方法上遇到问题,任何人都可以提供帮助吗?
首先你必须了解数组列表的基本用法。
您的删除实施有一个错误的假设:
public void removeMenu(Menu menumodel) {
for (Menu temp : menulist) {
if (temp.equals(menumodel)) {
menulist.remove(temp);
break;
}
}
}
等于:
public void removeMenu(Menu menumodel) {
menulist.remove(temp);
}
Arraylist 的实现将使用 hashCode() 和 equals() 来识别 arraylist 中的正确对象并为您删除它。因此,您不必遍历数组。
另请参阅如何从迭代列表中删除对象:Calling remove in foreach loop in Java
要更新数组列表中的对象,您首先必须检索对象然后更新它。这是因为 arrayslist 存储了对对象的引用而不是副本。
因此您的更新方法必须确定要更新的对象,然后相应地设置数量:
在我的示例中,您通过域名标识对象:此处为“菜单名称”,例如。咖啡。
public void updateMenu(int updateQuant, String name) {
for (Menu temp : menulist) {
if (temp.getName().equals(name)) {
temp.setQuant(updateQuant);
break;
}
}
}
此外,您已经在 UpdateProduct EventListener 中获得了正确的对象:
selectedProduct
这是 SAME 对象,因为它会被 updateMenu 方法找到。因此你可以调用
selectedProduct.setQuant(...)
此处,它也会更新数组列表。这是因为在不同的变量上存在对对象的引用。两者都引用同一个对象。
我正在尝试制作一个类似于此 removeMenu 方法的代码,但我想制作另一种方法来更新我订购的菜单中的产品数量。
餐厅管理员代码:
Menu coffee = new Menu("Coffee", 30);
Menu icedTea = new Menu("Iced Tea", 50);
Menu hotChoco = new Menu("Hot Chocolate", 30);
/*
constructor ommittet.
*/
private ArrayList<Menu> menulist;
public void removeMenu(Menu menumodel) {
for (Menu temp : menulist) {
if (temp.equals(menumodel)) {
menulist.remove(temp);
break;
}
}
}
public void updateMenu(int updateQuant) {
//menulist.set(3, updateQuant);
}
}
}
餐厅class
RestaurantController controller1 = new RestaurantController();
private void DeleteProductActionPerformed(java.awt.event.ActionEvent evt) {
selectedProduct = controller1.getMenulist().get(JTableOrderReceived.getSelectedRow());
if (selectedProduct != null) {
int ans = JOptionPane.showConfirmDialog(this, "The selected product will be removed! ", "DELETE PRODUCT", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
if (ans == JOptionPane.YES_OPTION) {
controller1.removeMenu(selectedProduct);
refTable();
}
}
}
private void UpdateProduct(java.awt.event.ActionEvent evt) {
selectedProduct controller1.getMenulist().get(JTableOrderReceived.getSelectedRow());
if (selectedProduct != null) {
int parseQuant = (int) selectedProduct.getQuantity();
String uQuant = JOptionPane.showInputDialog(this, "Update quantity " + selectedProduct.getItemName() + "\nPrice: " + selectedProduct.getPrice() + "\nCurrent order: " + parseQuant);
int nQuant = Integer.parseInt(uQuant);
controller1.updateMenu(nQuant);
refTable();
}
}
我在餐厅控制器 class 的 updateMenu 方法上遇到问题,任何人都可以提供帮助吗?
首先你必须了解数组列表的基本用法。
您的删除实施有一个错误的假设:
public void removeMenu(Menu menumodel) {
for (Menu temp : menulist) {
if (temp.equals(menumodel)) {
menulist.remove(temp);
break;
}
}
}
等于:
public void removeMenu(Menu menumodel) {
menulist.remove(temp);
}
Arraylist 的实现将使用 hashCode() 和 equals() 来识别 arraylist 中的正确对象并为您删除它。因此,您不必遍历数组。
另请参阅如何从迭代列表中删除对象:Calling remove in foreach loop in Java
要更新数组列表中的对象,您首先必须检索对象然后更新它。这是因为 arrayslist 存储了对对象的引用而不是副本。
因此您的更新方法必须确定要更新的对象,然后相应地设置数量:
在我的示例中,您通过域名标识对象:此处为“菜单名称”,例如。咖啡。
public void updateMenu(int updateQuant, String name) {
for (Menu temp : menulist) {
if (temp.getName().equals(name)) {
temp.setQuant(updateQuant);
break;
}
}
}
此外,您已经在 UpdateProduct EventListener 中获得了正确的对象:
selectedProduct
这是 SAME 对象,因为它会被 updateMenu 方法找到。因此你可以调用
selectedProduct.setQuant(...)
此处,它也会更新数组列表。这是因为在不同的变量上存在对对象的引用。两者都引用同一个对象。