我有两个java类,主要是构造函数,如getter和setter。
ReportGroup.java
String ReportGroupName;
List<ReportInfo> reportList; //getter & setter
ReportInfo.java
String ReportName;
String ReportId;
String ReportPath; //getter&setter
我在ReportGroup中设置了ReportInfo和ReportGroupName中的所有值。有没有可能的方法可以将ReportInfo的值添加到ReportGroup中的List(List reportList)
公共课堂考试{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 :(得分:0)
reportGroup.getReportList().add(reportInfo)
或
reportGroup.getReportList().addAll(reportInfos)
答案 1 :(得分:-1)
ReportInfo info = new ReportInfo();
// Do whatever is needed with the info object
如果您希望可以从任何地方访问相同的列表,则需要保持静态。
public static List<ReportInfo> reportList = new List<ReportInfo>();
这意味着reportList
中存储的值对于您为其创建的每个单独对象都不会有所不同。然后,您可以使用ReportGroup.reportList.add(info)
否则,请设置ReportGroup
的对象,只需确保reportList
公开
ReportGroup group = new ReportGroup();
group.reportList.add(info);