Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Intellij建议不使用变量名进行初始化_Java_Intellij Idea - Fatal编程技术网

Java 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

在进行我的项目时,我发现Intellij提出了这个建议。它删除了我的变量声明

Intellij将我的代码转换为:

    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);
    }