指令分配一个值,但该值不会在任何后续指令中读取或使用

发布于 2024-12-01 11:22:53 字数 597 浏览 3 评论 0原文

我有一段代码

for(int i = 0; i < num_of_random; i++){

    String str = in.readLine();
    if(str != null){
        String[] randoms = new String[4];
        randoms = str.split(",");

        dateRanges[i] = Integer.parseInt(randoms[0]);
        id[i] = Integer.parseInt(randoms[1]);
        flag[i] = Integer.parseInt(randoms[2]);
        system[i] = Integer.parseInt(randoms[3]);
    }
}

的建议

当我针对 findBugs 运行这段代码时,我得到了“String[] randoms = new String[4];”

该指令将一个值分配给局部变量,但该值不会在任何后续指令中读取或使用。通常,这表示错误,因为计算出的值从未被使用过。

为什么我会得到这个?

多谢

I have a piece of code

for(int i = 0; i < num_of_random; i++){

    String str = in.readLine();
    if(str != null){
        String[] randoms = new String[4];
        randoms = str.split(",");

        dateRanges[i] = Integer.parseInt(randoms[0]);
        id[i] = Integer.parseInt(randoms[1]);
        flag[i] = Integer.parseInt(randoms[2]);
        system[i] = Integer.parseInt(randoms[3]);
    }
}

When I run this code against findBugs, I get a suggestion for

"String[] randoms = new String[4];"

This instruction assigns a value to a local variable, but the value is not read or used in any subsequent instruction. Often, this indicates an error, because the value computed is never used.

Why do I get this?

Thanks a lot

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

绻影浮沉 2024-12-08 11:22:53

因为您将变量初始化为一个值 (new String[4]),然后将变量值替换为另一个值(str.split(",") 的结果>) 就在之后。因此初始化是不必要的。

您的代码在功能上等同于,

String[] randoms = str.split(",");

只不过它分配了一个新的 String 数组,该数组立即被丢弃。

Because you initialize a variable to a value (new String[4]), and then replace the variable value with another one (the result of str.split(",")) just after. The initialization is thus not necessary.

Your code is functionally equivalent to

String[] randoms = str.split(",");

except it allocates a new String array which is immediately discarded.

平生欢 2024-12-08 11:22:53

因为你可以简单地这样做:

String[] randoms = str.split(",");

Because you can simply do this:

String[] randoms = str.split(",");
篱下浅笙歌 2024-12-08 11:22:53

直接尝试:

String[] randoms = str.split(",");

您不需要实例化 String[]split 方法已经做到了。

Try directly:

String[] randoms = str.split(",");

You don't need to instanciate a String[], the split method already does it.

山有枢 2024-12-08 11:22:53

这是我称之为“我很笨”技术的一个例子。

通常,这是程序员编写代码但不理解他们在做什么的结果。在某些时候,程序员读到或听到这样的话:“你必须初始化所有局部变量!”。当他们编写代码时,String[] randoms 突然出现在他们的脑海中,因此他们添加了 = new String[4]

经验丰富的程序员可能会认为我曾经很愚蠢,但现在不再了!让我们将变量声明移出循环并生成如下内容:

String str;
String[] randoms;
for(int index = 0; index < num_of_random; ++index)
{
    str = in.readLine();
    if (str != null)
    {
        randoms = str.split(",");
        dateRanges[index] = Integer.parseInt(randoms[0]);
        id[index] = Integer.parseInt(randoms[1]);
        flag[index] = Integer.parseInt(randoms[2]);
        system[index] = Integer.parseInt(randoms[3]);
    }
}  

This is an example of the technique that I call The I'm dumb technique.

Typically it is the result of a programmer writing code, but not understanding what they are doing. At some point the programmer read or heard something like this: "you must initialize all local variables!". When they wrote the code String[] randoms that popped into their head so they added = new String[4].

A more seasoned programmer might look at that and think I used to be dumb, but not any more! Lets move the variable declarations out of the loop and produce something like this:

String str;
String[] randoms;
for(int index = 0; index < num_of_random; ++index)
{
    str = in.readLine();
    if (str != null)
    {
        randoms = str.split(",");
        dateRanges[index] = Integer.parseInt(randoms[0]);
        id[index] = Integer.parseInt(randoms[1]);
        flag[index] = Integer.parseInt(randoms[2]);
        system[index] = Integer.parseInt(randoms[3]);
    }
}  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文