英文:
what's the error in this for loop >> java netbeans
问题
这是一个显示我创建的表格中产品内容的方法。
public void Show_Products_In_Jatable(){
ArrayList<product> list = getProductList();
DefaultTableModel model = (DefaultTableModel) TTT.getModel();
Object[] row = new Object[4];
for (int i = 0; i<list.size() ; i++){
row[0] = list.get(i).getId();
row[1] = list.get(i).getName();
row[2] = list.get(i).getPrice();
row[3] = list.get(i).getAddDate();
model.addRow(row);
}
}
但是当我添加或删除其他产品时,它会自动发生,就像这张图片中显示的那样:图片链接。
也许问题出在这里:
public ArrayList<product> getProductList() {
ArrayList<product> productList = new ArrayList<product>();
Connection con = getConnection();
String query = "select * from products";
Statement st;
ResultSet rs;
try {
st = con.createStatement();
rs = st.executeQuery(query);
product product;
while(rs.next()){
product = new product(rs.getInt("id"), rs.getString("name"), Float.parseFloat(rs.getString("price")), rs.getString("add_date"), rs.getBytes("image"));
productList.add(product);
}
} catch (SQLException ex) {
Logger.getLogger(Main_Window.class.getName()).log(Level.SEVERE, null, ex);
}
return productList;
}
英文:
This is a method that displays the contents of the product in the table I did.
public void Show_Products_In_Jatable(){
ArrayList<product> list = getProductList();
DefaultTableModel model = (DefaultTableModel) TTT.getModel();
Object[] row = new Object[4];
for (int i = 0; i<list.size() ; i++){
row[0] = list.get(i).getId();
row[1] = list.get(i).getName();
row[2] = list.get(i).getPrice();
row[3] = list.get(i).getAddDate();
model.addRow(row);
}
}
But when I add or delete other products, it will automatically recur, as in the picture.
Maybe the problem is here
public ArrayList<product> getProductList() {
ArrayList<product> productList = new ArrayList<product>();
Connection con = getConnection();
String query = "select * from products";
Statement st;
ResultSet rs;
try {
st = con.createStatement();
rs = st.executeQuery(query);
product product;
while(rs.next()){
product = new product(rs.getInt("id"), rs.getString("name"), Float.parseFloat(rs.getString("price")), rs.getString("add_date"), rs.getBytes("image"));
productList.add(product);
}
} catch (SQLException ex) {
Logger.getLogger(Main_Window.class.getName()).log(Level.SEVERE, null, ex);
}
return productList;
}
答案1
得分: 0
你需要将行声明放在 for 循环内部,否则你只是创建了一个单一对象,该对象在每次循环中被修改,最终你的“model”对象将包含多个对同一对象的引用。
不过,如果你用 lambda 表达式替换整个行声明和 for 循环会更加简洁:
list.stream().forEach(i -> {
model.addRow(new Object[] {
i.getId(),
i.getName(),
i.getPrice(),
i.getAddDate()
});
});
干杯!
英文:
You will need to put the row declaration
Object[] row = new Object[4];
inside the for loop, otherwise you're creating just one single object which gets modified each time you cycle through the loop and eventually your "model" object will contain multiple references of the same object all over.
The whole row declaration and for loop would look a lot nicer if you replace it with lambdas though:
list.stream().forEach(i -> {
model.addRow(new Object[] {
i.getId(),
i.getName(),
i.getPrice(),
i.getAddDate()
});
});
Cheers!
答案2
得分: 0
请尝试这样做:
public void Show_Products_In_Jatable(){
ArrayList<product> list = getProductList();
DefaultTableModel model = (DefaultTableModel) TTT.getModel();
for (int i = 0; i<list.size() ; i++){
Object[] row = new Object[4];
row[0] = list.get(i).getId();
row[1] = list.get(i).getName();
row[2] = list.get(i).getPrice();
row[3] = list.get(i).getAddDate();
model.addRow(row);
}
}
英文:
Try this:
public void Show_Products_In_Jatable(){
ArrayList<product> list = getProductList();
DefaultTableModel model = (DefaultTableModel) TTT.getModel();
for (int i = 0; i<list.size() ; i++){
Object[] row = new Object[4];
row[0] = list.get(i).getId();
row[1] = list.get(i).getName();
row[2] = list.get(i).getPrice();
row[3] = list.get(i).getAddDate();
model.addRow(row);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论