这个for循环中有什么错误? >> Java NetBeans

huangapple go评论73阅读模式
英文:

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&lt;product&gt; list = getProductList();
    DefaultTableModel model =  (DefaultTableModel) TTT.getModel();
    
    Object[] row = new Object[4];
    for (int i = 0; i&lt;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&lt;product&gt; getProductList() {

    ArrayList&lt;product&gt; productList = new ArrayList&lt;product&gt;();
    Connection con = getConnection();
    String query = &quot;select * from products&quot;;

    Statement st;
    ResultSet rs;

    try {
        st = con.createStatement();
        rs = st.executeQuery(query);
        product product;
        
        while(rs.next()){
            product = new product(rs.getInt(&quot;id&quot;), rs.getString(&quot;name&quot;), Float.parseFloat(rs.getString(&quot;price&quot;)), rs.getString(&quot;add_date&quot;), rs.getBytes(&quot;image&quot;));
            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 -&gt; {
  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&lt;product&gt; list = getProductList();
    DefaultTableModel model =  (DefaultTableModel) TTT.getModel();
    for (int i = 0; i&lt;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);             
    }
}

huangapple
  • 本文由 发表于 2020年7月26日 16:15:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/63097716.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定