How to get a list of specific fields values from objects stored in a list?

后端 未结 12 1322
长发绾君心
长发绾君心 2021-01-31 08:23

Say I have an list of objects with two fields field1 and field2, both of String type.

How do I get a list of all field1 values wit

12条回答
  •  天命终不由人
    2021-01-31 08:46

    Add the values of records in different lists and by using the one or two iterator simply print the seperate values.like follows:

    rs=st.executeQuery("select * from stu");
    List data=new ArrayList();
    List data1=new ArrayList();
    while(rs.next())
    {
       data.add(rs.getString(1));
       data1.add(rs.getString(2));
    }
    Iterator it=data.iterator();
    Iterator it1=data1.iterator();
    while(it1.hasNext())
    {
       System.out.println(" "+it.next()+"  "+it1.next());
    }
    

提交回复
热议问题