Java Intellij建议不使用变量名进行初始化
在进行我的项目时,我发现Intellij提出了这个建议。它删除了我的变量声明 Intellij将我的代码转换为:Java Intellij建议不使用变量名进行初始化,java,intellij-idea,Java,Intellij Idea,在进行我的项目时,我发现Intellij提出了这个建议。它删除了我的变量声明 Intellij将我的代码转换为: public void start() throws IOException { JSONObject yearJson = new JSONObject(); JSONObject regionJson = new JSONObject(); 到 这是我的全部代码: public void start() throws IOExc
public void start() throws IOException {
JSONObject yearJson = new JSONObject();
JSONObject regionJson = new JSONObject();
到
这是我的全部代码:
public void start() throws IOException {
new JSONObject();
new JSONObject();
JSONArray yearArray = new JSONArray();
String basePath = "/Users/andrei/AplicatieLicenta/censusOutput/";
StringBuilder path = new StringBuilder();
File file;
BufferedReader bufferedReader;
JSONObject yearJsonObject = new JSONObject();
for (String year : years) {
yearJsonObject = new JSONObject();
yearJsonObject.put("year", year);
JSONArray jsonArray = new JSONArray();
for (String region : regions) {
path = new StringBuilder();
path.append(basePath).append(year).append("/").append(region).append("/result");
file = new File(path.toString());
bufferedReader = new BufferedReader(new FileReader(file));
JSONObject regionData = new JSONObject();
regionData.put("region", region);
String line;
JSONArray values = new JSONArray();
while ((line = bufferedReader.readLine()) != null) {
JSONObject demographics = new JSONObject();
demographics.put("value", getJsonFromLine(line));
values.add(demographics);
}
regionData.put("demographics", values);
jsonArray.add(regionData);
yearJsonObject.put("data", jsonArray);
}
yearArray.add(yearJsonObject);
}
parentJson.put("years", yearArray);
System.out.println(parentJson);
}
它仍然工作,做完全相同的事情,但我不明白为什么。我在网上搜索了一下,没有找到任何东西,甚至没有找到。这些都是未使用的变量,因此IntelliJ建议删除它们。调用
newjsonobject()
可能有副作用,所以它保留了这些副作用;你可以删除它们。(最好使用类似Jackson的东西,使JSON处理更简单。)我认为原因是这两个变量从未使用过。但是intellij不只是删除它们,而是意识到当它们被初始化时,会发生一些事情(ew JSONObject()
)。因此,删除整行代码也会删除“内容”,这正是intellij试图避免的。
public void start() throws IOException {
new JSONObject();
new JSONObject();
JSONArray yearArray = new JSONArray();
String basePath = "/Users/andrei/AplicatieLicenta/censusOutput/";
StringBuilder path = new StringBuilder();
File file;
BufferedReader bufferedReader;
JSONObject yearJsonObject = new JSONObject();
for (String year : years) {
yearJsonObject = new JSONObject();
yearJsonObject.put("year", year);
JSONArray jsonArray = new JSONArray();
for (String region : regions) {
path = new StringBuilder();
path.append(basePath).append(year).append("/").append(region).append("/result");
file = new File(path.toString());
bufferedReader = new BufferedReader(new FileReader(file));
JSONObject regionData = new JSONObject();
regionData.put("region", region);
String line;
JSONArray values = new JSONArray();
while ((line = bufferedReader.readLine()) != null) {
JSONObject demographics = new JSONObject();
demographics.put("value", getJsonFromLine(line));
values.add(demographics);
}
regionData.put("demographics", values);
jsonArray.add(regionData);
yearJsonObject.put("data", jsonArray);
}
yearArray.add(yearJsonObject);
}
parentJson.put("years", yearArray);
System.out.println(parentJson);
}