“The array is only written to, never read from”错误显示在Java代码中。

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

"The array is only written to, never read from" error showing in Java code

问题

以下是翻译好的部分:

我一直在努力提升一些Java编程能力,正试图复习一下基础知识,所以我一直在学习《山姆的24小时Java编程》这本书。其中一个项目是为了熟悉如何描述对象,代码如下:

package com.java24hours;

public class GremlinLab {
    public static void main(String[] arguments) {
        int numGremlins = Integer.parseInt(arguments[0]);
        if (numGremlins > 0) {
            Gremlin[] gremlins = new Gremlin[numGremlins];
            for (int i = 0; i < numGremlins; i++) {
                gremlins[i] = new Gremlin();
            }
            System.out.println("有 " + Gremlin.getGremlinCount()
                + " 个小精灵。");
        }
    }
}

在第7行编译时出现错误:“数组只有写入操作,没有读取操作”:

Gremlin[] gremlins = new Gremlin[numGremlins];

我无法想明白为什么这个地方不起作用!如果我太愚蠢的话,请原谅我,但是任何帮助将不胜感激 “The array is only written to, never read from”错误显示在Java代码中。 谢谢!

抱歉,我迅速编辑以添加我没有提到的另一个类!!

我还有以下这个类:

package com.java24hours;

public class Gremlin {
    static int gremlinCount = 0;

    public Gremlin() {
        gremlinCount++;
    }

    static int getGremlinCount() {
        return gremlinCount;
    }
}

然后通过自定义项目配置来指定命令行参数。

最后一次编辑!!…
非常感谢大家,我真的非常感激。我在使用集成开发环境(IDE)并将这些类移动到源代码包文件夹,而不是“com.java24hours”包中...原始代码竟然正常工作了?!显然我是个新手!再次感谢大家 “The array is only written to, never read from”错误显示在Java代码中。

英文:

I've been trying to polish up on some Java and I'm trying to refresh the basics so I've been working through the Sam's 24 hour java book. One of the projects to familiarize yourself with describing objects gives the code as follows:

package com.java24hours;

public class GremlinLab {
    public static void main(String[] arguments) {
        int numGremlins = Integer.parseInt(arguments[0]);
        if (numGremlins &gt; 0) {
            Gremlin[] gremlins = new Gremlin[numGremlins];
            for (int i = 0; i &lt; numGremlins; i++) {
                gremlins[i] = new Gremlin();
            }
            System.out.println(&quot;There are &quot; + Gremlin.getGremlinCount()
                + &quot; gremlins.&quot;);
        }
    }
}

The error "The array is only written to, never read from" is showing upon compilation in the 7th line:

Gremlin[] gremlins = new Gremlin[numGremlins];

and I can't figure out for the life of me why this is not working! Apologies if I'm being stupid but any help will be greatly appreciated “The array is only written to, never read from”错误显示在Java代码中。 Thanks!

Sorry quick edit to add the other class I haven't mentioned!!

I have also got the following class:

package com.java24hours;

public class Gremlin {
    static int gremlinCount = 0;

    public Gremlin() {
        gremlinCount++;
    }

    static int getGremlinCount() {
        return gremlinCount;
    }
}

I then specified the command-line arguments by customizing the project configuration.

Last Edit!!...
Thanks very much everyone, I really appreciate it. I'm using an IDE and moved the classes to the source packages folder rather than the 'com.java24hours' package....and my original codes have worked fine?! Clearly I'm very much a novice! Thanks again all “The array is only written to, never read from”错误显示在Java代码中。

答案1

得分: 3

这只是一个警告,而不是错误。您可以安全地忽略它,您的程序将按您的意图运行。

这个警告是因为您正在创建一个数组并填充它有元素,但是您实际上从未使用过这个数组或数组中的元素。例如,您可以这样做:

System.out.println("数组中有 " + gremlins.length + " 个小精灵。");

以打印数组的长度。这将消除警告。

更好的方法是,如果您是在编写这个代码作为练习来学习关于“static”方法和变量,干脆不要使用数组。

英文:

This is a warning, not an error. You can safely ignore it and your program will function as you intend.

The warning is because you are creating an array and filling it with elements. But then you never actually use the array nor the elements of that array for anything. For example, you could do

System.out.println(&quot;There are &quot; + gremlins.length + &quot; gremlins.&quot;);

to print the length of the array. This will remove the warning.

Better yet, if you are writing this as an exercise to learn about static methods and variables, just don't use an array at all.

答案2

得分: 0

你没有对 gremlins 数组的元素进行任何操作。也就是说,在你的代码中没有类似 gremlins[i].someMethod() 这样的操作,但这只是一个警告。

英文:

You are not doing anything with the elements of the gremlins array. ie nowhere does your code do something like gremlins[i].someMethod(), but it's only a warning.

huangapple
  • 本文由 发表于 2020年8月26日 06:11:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/63587809.html
匿名

发表评论

匿名网友

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

确定