SonarLint,字符串数组:使用“var”代替=错误声明此局部变量
使用Java 11, 对于此代码:
String[] arrayString = {"foo", "bar"};
SonarLint 说 Declare this local variable with "var" instead.
所以,我试过:
var arrayString = {"foo", "bar"};
// or
var[] arrayString = {"foo", "bar"};
但现在我收到这些错误:
Array initializer needs an explicit target-type'var' is not allowed as an element type of an array
如何正确声明数组变量或属性。
回答
你可以用
var arrayString = new String[]{"foo", "bar"};
- Lol. "Use `var` so you don't have to declare the type!", they said.