简体   繁体   English

为什么变量初始化是多余的?

[英]Why is variable initialisation redundant?

I've solved a 3*N + 1 problem (100) on UVa Online Judge but here's one thing that I don't understand, but it has something to do with Java, I guess, not with the algorithm itself. 我已经在UVa Online Judge上解决了一个3 * N + 1问题(100),但这是我不明白的一件事,但它与Java有关,我想,不是算法本身。 So here's the code: 所以这是代码:

private static int maxCycleLength(int lo, int hi) {
        maxLength = 0;
        int n = 0;
        int length = 0;

    for (int i = lo; i <= hi; i++) {
        // It's the first time we're computing the cycle length for n
        if (lengths[i] == 0) {
            n = i;
            length = 1;
            while (n != 1) {
                if ((n % 2) == 0) {
                    n = n >> 1;
                    length++;
                }
                else {
                    n = 3 * n + 1;
                    n = n >> 1;
                    length += 2;
                }
            }
            lengths[i] = length;
        }
        // Otherwise we just look it up
        else
            length = lengths[i];

        // Check if the current cycle length is the maximum
        if (length > maxLength)
            maxLength = length;
    }
    return maxLength;

What I don't understand: My IDE (IDEA) tells me that in this piece of code the initialisation of variables n and length is redundant, however, maxLength must be initialised and if I don't do it, it does not compile. 我不明白:我的IDE(IDEA)告诉我,在这段代码中,变量nlength的初始化是多余的,但是,必须初始化maxLength ,如果我不这样做,它就不会编译。

Why so? 为什么这样? How is maxLength different here from n and length ? 这里的maxLengthnlength什么不同?

On all code paths, you initialize n and length before you ever try and read a value from them. 在所有代码路径上,在尝试从中读取值之前,先初始化nlength Regardless of what happens, the value you assign to them initially will not be used, since you'll overwrite it with something else before you need it. 无论发生什么,最初分配给它们的值都不会被使用,因为在你需要它之​​前你会用其它东西覆盖它。

maxLength though gets read from before you assign a new value to it, when you do the length > maxLength comparison, or in the return , if the for loop is skipped. maxLength虽然在为其分配新值之前读取,但是当您执行length > maxLength比较时,或者在return ,如果跳过for循环。 Since it won't have a value on the first pass, you need to give it a starting value to use. 由于它在第一遍中没有值,因此您需要为其提供一个起始值。

maxLength is different because its value is used outside the for loop. maxLength不同,因为它的值在for循环之外使用。 If the for loop is never entered, maxLength will never be initialized (if you remove the initialization at the start), so the return statement becomes invalid. 如果永远不输入for循环,则永远不会初始化maxLength (如果在开始时删除初始化),则return语句将变为无效。

The other two variables ( n and length ) are guaranteed to be assigned before they are accessed, even after removing the initial initializations. 保证在访问它们之前分配其他两个变量( nlength ),即使在删除初始初始化之后也是如此。 They are not accessed outside the for loop, and inside the for loop, both the if and else clauses start by initializing length . 它们不在for循环之外访问,而在for循环内, ifelse子句都是从初始化length开始的。 n is initialized only by the if cluase, but it's only accessed within that clause. n仅由if cluase初始化,但只能在该子句中访问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM