如何迭代对象列表中的列值?

时间:2013-03-19 05:31:27

标签: java

这是我的代码:

 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 个答案:

答案 0 :(得分: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();
        }
    }
}