如何将这个Java POJO在for循环中保存到Repository中?

huangapple go评论86阅读模式
英文:

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(&quot;Company_Name_1&quot;);
Company c2 = new Company(&quot;Company_Name_2&quot;);
Company c3 = new Company(&quot;Company_Name_3&quot;);

for (int i = 1; i &lt; 4; i++ ) {
    try {
        companyRepository.save(&quot;c&quot; + i); // this!
    } catch ( Exception ex ) {
        System.out.println(ex.toString());
    }
}

I've also tried

companyRepository.save(&quot;c&quot; + String.valueOf(i));   // error

companyRepository.save(&quot;c&quot; + 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 -&gt; {
    try {
        companyRepository.save(c);
    } catch (Exception ex) {
        System.out.println(ex.toString());
    }
})
英文:
Arrays.asList(c1, c2, c3).forEach(c -&gt; {
	try {
		companyRepository.save(c);
	} catch ( Exception ex ) {
		System.out.println(ex.toString());
	}
})

huangapple
  • 本文由 发表于 2020年10月24日 10:52:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/64509503.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定