有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

stringbuilder使用字符串生成器和数组在java中循环

我试图使用一个字符串生成器来附加第一个文件及其位置,然后是新行第二个文件及其位置等。我该怎么做?正确的语法是什么?我的下环怎么了

@Override
public List<FileUpload> uploadFile(MultipartFile[] files, String windowsUsername, String number) {
List<FileUpload> uploadList = new ArrayList<FileUpload>();
        for (MultipartFile file : files) {

                FileUpload result = itsmFileService.uploadAttachment(file, number);


                uploadList.add(result);     



        }
        String supportCallID;
        supportCallID = this.getSupportCallIDForTicketNumber(number);

int i = 0;
            for (FileUpload loopLocation : uploadList){
                notesSection = uploadList.get(i).getLocation();
                StringBuilder sb = new StringBuilder();
                sb.append(notesSection);
                sb.toString();
            }

    }

}


共 (1) 个答案

  1. # 1 楼答案

    每次循环运行时,您都会创建一个新的StringBuilder。尝试以下方法:

    StringBuilder sb = new StringBuilder();
    
    for (FileUpload loopLocation : uploadList){
         string notesSection = uploadList.get(i).getLocation();
    
         sb.append(notesSection);
    }
    
    sb.toString();