2013-03-19 61 views
0

这里是我的代码:如何迭代对象列表中的列值?

public String generateEISReports_PDF(){ 

    surveyReportService.setSurveyReportDA(surveyReportDA); 
    surveyReportList = surveyReportService.getSurveyReport(surveyType, surveyDate, projectCode, employeeCode); 
    if(surveyReportList != null){ 

     System.out.println(surveyReportList + "testing"); 
     System.out.println(surveyReportList.size() + "size ito"); 

     for (SurveyReport surveyReport : surveyReportList) {     
      System.out.println(surveyReport.getRiskRank().toString() + "asdf"); 
      surveyReports.add(surveyReport); 
     } 

    } 

    this.compileTheJasperReports(); 
    return SUCCESS; 
} 

我得到了全行的值与此代码作为一个对象。我想迭代每个对象列表中的字段值。我怎样才能做到这一点?

+1

我想你已经在你的代码中使用for循环了。 – SudoRahul 2013-03-19 05:34:38

+1

我不明白。请你解释一下 – PSR 2013-03-19 05:37:09

+0

我的意思是,我想得到每个列表中每个列表的字段值。我没有把它做对。我的代码中发生的事情是,它只是迭代整个列表,整个行的值。 – 2013-03-19 05:40:28

回答

0

通过使用反射,你可以实现这一点。以下是代码。这可能会帮助你。

import java.lang.reflect.Field; 
import java.util.ArrayList; 
import java.util.Iterator; 
import java.util.List; 

public class SurveyReport { 
    public int localServeryCont; // if you make it private then changes have to be done in 'SurveyIterator' inner class. 
    public String surveyReporterName; 

    public static void main(String[] args) { 
     List<SurveyReport> surveyReportList = new ArrayList<SurveyReport>(); 
     SurveyReport sr = new SurveyReport(); //first object creation 
      sr.localServeryCont = 10; 
      sr.surveyReporterName = "AAA"; 
      surveyReportList.add(sr); 

     sr = new SurveyReport(); //second object creation 
      sr.localServeryCont = 100; 
      sr.surveyReporterName = "BBB"; 
      surveyReportList.add(sr); //two objects are in the list-object. 

     for (SurveyReport surveyReport : surveyReportList) { 
      Iterator<String> itr = surveyReport.iterator(); //You can work around with 'java.lang.Iterable' to use 'foreach' loop 
      while (itr.hasNext()) { //this is what you might be expecting 
       System.out.println("SurveyReport's object's values : " + itr.next()); 
      } 

     } 
    } 

    public Iterator<String> iterator() { //here is method to get iterator object. 
     return new SurveyIterator(); 
    } 

    private class SurveyIterator implements Iterator<String> { //creating 'SurveyIterator' INNER class 
     private int totalAvailableField = SurveyReport.class.getDeclaredFields().length; 
     int cursor = 0; 
     Field[] surveyReportFields = SurveyReport.class.getFields(); 

     @Override 
     public boolean hasNext() { 
      return cursor != totalAvailableField; 
     } 

     @Override 
     public String next() { 

      String next = null; 
      try { 
       next = (surveyReportFields[cursor].get(SurveyReport.this)).toString(); 
      } catch (IllegalArgumentException e) { 
       e.printStackTrace(); 
      } catch (IllegalAccessException e) { 
       e.printStackTrace(); 
      } 
      cursor++; 
      return next; 
     } 

     @Override 
     public void remove() { 
      throw new UnsupportedOperationException(); 
     } 
    } 
}