简体   繁体   English

在循环内定义的Java变量似乎无法在循环外识别?

[英]Java variable defined inside a loop doesn't seem to be recognized outside the loop?

I have a section of code that is puzzling me. 我有一段代码令人困惑。 I define an integer array inside an if/else statement because the length of the array depends on the length of 2 inputs to the method. 我在if / else语句中定义一个整数数组,因为该数组的长度取决于该方法的2个输入的长度。 My problem is that outside the if/else statement, the variable definition seems to be lost. 我的问题是,在if / else语句之外,变量定义似乎丢失了。

import java.util.Arrays;

public class test {

  public String AddArrays(int [] arg1, int [] arg2) {
    int L1 = arg1.length;
    int L2 = arg2.length;
    if (L1 > L2) {
        int[] output = new int[L2];
        for (int i = 0; i < L2; i++) {
            output[i] = arg1[i] + arg2[i];
        }
    } else {
        int[] output = new int[L1];
        for (int i = 0; i < L2; i++) {
            output[i] = arg1[i] + arg1[i];
        }
    }
    String result = Arrays.toString(output);
    return result;
    }
}

The error I get is on the statement String result = Arrays.toString(output); 我得到的错误是在语句String result = Arrays.toString(output); where Eclipse tells me that output cannot be resolved to a variable. Eclipse告诉我output无法解析为变量。

...and by the way, yes, I know that this is not the way to add two integer arrays -- I reduced this from more complex code to demonstrate the problem! ...而且顺便说一句,是的,我知道这不是添加两个整数数组的方法-我从更复杂的代码中简化了此过程以演示问题!

Define output before if statement. if语句之前定义output Like this: 像这样:

int[] output;
int L1 = arg1.length;
int L2 = arg2.length;
if (L1 > L2) {
    output = new int[L2];
    for (int i = 0; i < L2; i++) {
        output[i] = arg1[i] + arg2[i];
    }
} else {
    output = new int[L1];
    for (int i = 0; i < L2; i++) {
        output[i] = arg1[i] + arg1[i];
    }
}
String result = Arrays.toString(output);
return result;
}

When you declared output inside the if statement it simply had only that block scope. if语句中声明output ,它仅具有该块作用域。

Scope of a variable is always the next enclosing { } . 变量的范围始终是下一个封闭{ }

Of cause starting at its declaration (not at the { ) 原因从声明开始(而不是在{ )开始

Well, you have already got the solution, but I would like to point out that, you can reduce your methods, to avoid duplicating codes, which you are currently doing. 好的,您已经有了解决方案,但是我想指出的是,您可以减少方法,避免重复执行当前正在执行的代码。

You can make use of conditional operators to create the array according to the result of L1 > L2 . 您可以根据L1 > L2的结果使用conditional operators来创建数组。 And rather than iterating till L1 or L2 , you should iterate till the length of array output . 而且,您应该迭代直到数组output的长度,而不是迭代到L1L2

So, you can try using the below code: - 因此,您可以尝试使用以下代码:-

public String addArrays(int [] arg1, int [] arg2) {
    int L1 = arg1.length;
    int L2 = arg2.length;

    int[] output = L1 > L2 ? new int[L2]: new int[L1];

    for (int i = 0; i < output.length; i++) {
        output[i] = arg1[i] + arg2[i];
    }

    return Arrays.toString(output);
}

And please follow Java Naming Conventions. 并且请遵循Java命名约定。 Method name should start with lowercase alphabets. 方法名称应以小写字母开头。

You are declaring the output variable as a local variable inside each of the if/else statements. 您在每个if / else语句中将输出变量声明为局部变量。 To fix this, declare it first outside then adjust it and return the results. 要解决此问题,请先在外部声明它,然后对其进行调整并返回结果。 This keeps it in the scope between the brackets { } . 这使它处于方括号{ }之间的范围内。

public String AddArrays(int [] arg1, int [] arg2) {
    int L1 = arg1.length;
    int L2 = arg2.length;
    int[] output;
    if (L1 > L2) {
        output = new int[L2];
        for (int i = 0; i < L2; i++) {
            output[i] = arg1[i] + arg2[i];
        }
    } else {
        output = new int[L1];
        for (int i = 0; i < L2; i++) {
            output[i] = arg1[i] + arg1[i];
        }
    }
    String result = Arrays.toString(output);
    return result;
}

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

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