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;
}

このコードをオブジェクトとして、行全体の値を取得しています。オブジェクトのすべてのリストでフィールド値を繰り返したい。どうやってやるの?

4

1 に答える 1

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();
        }
    }
}
于 2013-03-19T09:37:45.337 に答える