英文:
How do I save this Java POJO in for loop into a Repository?
问题
我正在尝试循环创建 Java POJO,但是 IntelliJ 出现了错误。
Company c1 = new Company("Company_Name_1");
Company c2 = new Company("Company_Name_2");
Company c3 = new Company("Company_Name_3");
for (int i = 1; i < 4; i++) {
try {
companyRepository.save("c" + i); // 这里出错了!
} catch (Exception ex) {
System.out.println(ex.toString());
}
}
我还尝试过:
companyRepository.save("c" + String.valueOf(i)); // 出错了
companyRepository.save("c" + Integer.getInteger(i)); // 出错了
错误显示类型参数 'S' 的推断类型 'S' 不在限定范围内。
对于如何修复此错误,有任何想法吗?
英文:
I'm trying to loop over Java POJO creation but intellij is giving errors.
Company c1 = new Company("Company_Name_1");
Company c2 = new Company("Company_Name_2");
Company c3 = new Company("Company_Name_3");
for (int i = 1; i < 4; i++ ) {
try {
companyRepository.save("c" + i); // this!
} catch ( Exception ex ) {
System.out.println(ex.toString());
}
}
I've also tried
companyRepository.save("c" + String.valueOf(i)); // error
companyRepository.save("c" + Integer.getInteger(i)); // error
Error says Inferred Type 'S' for type parameter 'S' is not within bounds.
Any ideas on fixing this error?
答案1
得分: 1
Arrays.asList(c1, c2, c3).forEach(c -> {
try {
companyRepository.save(c);
} catch (Exception ex) {
System.out.println(ex.toString());
}
})
英文:
Arrays.asList(c1, c2, c3).forEach(c -> {
try {
companyRepository.save(c);
} catch ( Exception ex ) {
System.out.println(ex.toString());
}
})
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论