如何在Java中迭代List列表并打印元素

时间:2018-10-08 15:55:10

标签: list

请告知如何迭代以下列表,并获取客户名称,余额和总金额的详细信息。

列出方法:

List<List> invoiceGroupByCustomer() throws SQLException {
        List<List> list = new ArrayList<List>();
        String query1 = "select customer_name,sum(total_amount),sum(balance) from invoice GROUP BY customer_name ORDER BY customer_name";
        try
        {
            DbConnection obj = new DbConnection();
            Connection con = obj.getConnection(); 
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery(query1);
            while(rs.next())
            {   
                Invoice invoiceobj = new Invoice();
                invoiceobj.setCustomerName(rs.getString("customer_name"));
                invoiceobj.setTotalAmount(rs.getDouble("sum(total_amount)"));
                invoiceobj.setBalance(rs.getDouble("sum(balance)"));    
                list.add(Arrays.asList(invoiceobj));
            }

        }catch(Exception e)
        {
            e.printStackTrace();
        }
        return list;

    }

主要方法:(尝试进行迭代)

case 4:         
System.out.println("Invoice Summary");
System.out.println(String.format("%-17s %-15s %s", "Customer Name","Total Amount","Total Balance"));
List<List> list4= invoicedaoObj.invoiceGroupByCustomer();              
for(List i : list4)
{
for(Invoice l : i)
{
System.out.format("%-17s %-15s %s\n",i.getCustomerName(),i.getTotalAmount(),i.getBalance());
}
}
break;

0 个答案:

没有答案