2014-10-21 6 views
0

나는 두 개의 자바 클래스를 가지고 있는데, 대부분 getter와 setters 같은 생성자이다.한 클래스의 객체를 다른 클래스의 arraylist에 추가하기

ReportGroup.java 

    String ReportGroupName; 
    List<ReportInfo> reportList; //getter & setter 

ReportInfo.java 

    String ReportName; 
    String ReportId; 
    String ReportPath; //getter&setter 

내가 거기 ReportGroup.Is에 내가 ReportGroup (목록 reportList)

공용 클래스 테스트 {의 목록에 ReportInfo의 값을 추가 할 수있는 모든 가능한 방법을 ReportInfo 및 ReportGroupName의 모든 값을 설정 한

public static List<ReportInfo> reportInfo = new ArrayList<ReportInfo>(); 

public static void main(String[] args) throws IOException { 
    String orgId="346"; 
    String orgName="Ralph Lauren Corporation"; 

    String reportPath= "/"+orgId +"_"+orgName.replaceAll(" ","_"); 
    String reportFolderPath = "C:/Lokesh/Dev Tools/apache-tomcat-6.0.37/webapps/ROOT/Reports"; 
    List<String> filesArray = null; 
    filesArray = new ArrayList<String>(); 

    String filetowrite="C:/Users/lbandhu/Desktop/test.html"; 
    FileWriter fw=new FileWriter(filetowrite); 


    ReportGroup reportGroup=new ReportGroup(); 
    reportGroup.setReportGroupName(orgName); 
    reportGroup.getReportList().addAll(reportInfo); 
    File folder = new File(reportFolderPath+reportPath); 

    List<String> reportNames = listFolder(folder); 



    for (String rname : reportNames) { 
     ReportInfo reportInfo=new ReportInfo(); 
     reportInfo.setReportName(rname); 

     folder = new File(reportFolderPath+reportPath+"/"+rname); 
     System.out.println(folder); 
     fw.write("<br><tr> "+rname +" Utilization Reports</tr><br>"); 
     Iterator<String> iterator=listFilesForFolder(folder, filesArray).iterator(); 
     writeReportPath(fw,iterator, reportFolderPath,reportInfo); 
     fw.write("<br>"); 

    } 

    filesArray.clear(); 

    fw.close(); 

private static void writeReportPath(FileWriter fw,Iterator<String> iterator, String reportFolderPath,ReportInfo reportInfo) throws IOException 
{ 

    while(iterator.hasNext()){ 
     String listofFiles=iterator.next(); 

     StringBuilder strgfile=new StringBuilder(listofFiles); 
     String filename=strgfile.substring(strgfile.indexOf("/")+1); 

     //System.out.println("name" +filename); 
     String[] split = filename.split("_"); 

       if(split.length > 1) { 
        if(split[0].contains("MONTHLY")) { 
         //System.out.println(split[1].substring(0, 3) + " " + split[2]); 
         String monthly=split[1]+ " " + split[2]; 
         int index=monthly.indexOf('/'); 
         String reportId=monthly.substring(0, index); 
         //ReportInfo reportInfo=new ReportInfo(); 
         //List <ReportInfo> reportGroup=new ArrayList<ReportInfo>(); 
         reportInfo.setReportId(reportId); 
         reportInfo.setReportPath(reportFolderPath + "/" + strgfile); 


         fw.write("<br><tr><a href="+"\"" + reportFolderPath + "/" + strgfile+"\""+"/>" 
         +reportId+"</a></tr>"); 
        } 
        else { 
         for(int i = 1; i < split.length; i++) { 
          //String org=+rname; 
          String title2=split[i] + " "; 


         } 
         //System.out.println(""); 
        } 
       } 
       else { 

        fw.write("<br>"+"<h>"+filename +"</h>"+"<br>"); 
       } 
      } 
     } 

답변

0
reportGroup.getReportList().add(reportInfo) 

또는

reportGroup.getReportList().addAll(reportInfos) 
,
+0

이것은'ReportGroup'과'ReportInfo' 모두의 인스턴스를 필요로하게 있는지 확인 ReportGroup의 대상을, 그렇지 않으면 ReportGroup.reportList.add(info)

를 사용할 수 있습니다. –

+0

감사합니다. – Lokesh

-1
ReportInfo info = new ReportInfo(); 
// Do whatever is needed with the info object 

어디서나 같은 목록에 액세스 할 수 있도록하려면 정적이어야합니다.

public static List<ReportInfo> reportList = new List<ReportInfo>(); 

즉, reportList에 저장된 값은 사용자가 만든 개별 개체마다 다릅니다. 그런 다음 당신이 reportList 공공

ReportGroup group = new ReportGroup(); 
group.reportList.add(info); 
+0

아니요, 클래스가 구조화되어있어 정적 액세스를 원하지 않으며 정적으로 만들지도 않습니다. –

+0

@ JoshM 따라서 두 번째 옵션을 추가했습니다 ... 투표를하기 전에 또는 부정적인 의견을 남기 전에 전체 답변을 읽어보십시오 – SamTebbs33

+0

@ SamTebbs33 두 번째 옵션을 추가했을 수도 있지만 '정적'부분은 더 이상 이해가되지 않습니다 전보다. – bcsb1001

관련 문제