简体   繁体   English

将一个类的对象添加到另一类的数组列表中

[英]Adding object of one class to the arraylist in another class

I have two java class,mostly constructors such as getter and setters. 我有两个java类,主要是getter和setters等构造函数。

ReportGroup.java

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

ReportInfo.java

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

I have set all the values in ReportInfo and ReportGroupName in ReportGroup.Is there any possible way where I can add the values of ReportInfo to the List in ReportGroup( List reportList) 我已经在ReportGroup的ReportInfo和ReportGroupName中设置了所有值。是否有任何可能的方法可以将ReportInfo的值添加到ReportGroup中的List(List reportList)

public class test { 公开课测试{

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>");
                }
            }
       }
reportGroup.getReportList().add(reportInfo)

要么

reportGroup.getReportList().addAll(reportInfos)
ReportInfo info = new ReportInfo();
// Do whatever is needed with the info object

If you want the same list to be accessible from anywhere, it needs to be static. 如果您希望可以从任何地方访问同一列表,则该列表必须是静态的。

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

This means that the values stored inside reportList will not differ for each individual object you create for it. 这意味着存储在reportList中的值对于您为其创建的每个对象都不会有所不同。 You can then use ReportGroup.reportList.add(info) 然后,您可以使用ReportGroup.reportList.add(info)

Otherwise, make an object of ReportGroup , just make sure that you make reportList public 否则,使ReportGroup为对象,只需确保将reportList公开

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM