调用一个变量到另一个 Java 方法中

huangapple 未分类评论47阅读模式
英文:

Calling a variable into another method in java

问题

public static void readDataArray(String filename) throws FileNotFoundException {
Scanner input = new Scanner(new BufferedReader(new FileReader("sample.xyz")));
int rows = 2500000;
int columns = 3;

double[][] arrayOfEarth = new double[rows][columns];
while (input.hasNextLine()) {
    for (int a = 0; a < arrayOfEarth.length; a++) {
        String[] line = input.nextLine().trim().split("\t");
        for (int b = 0; b < line.length; b++) {
            arrayOfEarth[a][b] = Double.parseDouble(line[b]);
        }
    }
}
System.out.println(Arrays.deepToString(arrayOfEarth));

}

public static List CoordinatesAbove(double altitude) {
List CoordinatesAbove = new ArrayList<>();
for (int c = 0; c < arrayOfEarth.length; c++) {
CoordinatesAbove.add(arrayOfEarth[c][2]);
}
CoordinatesAbove.removeIf(d -> d < altitude);
return CoordinatesAbove;
}

英文:

Does anyone know how to call a variable into another method? Doing an assignment for school and really struggling. (Variable that I want to call is arrayOfEarth by the way.)

 public static void readDataArray(String filename) throws FileNotFoundException {
    Scanner input = new Scanner(new BufferedReader(new FileReader(&quot;sample.xyz&quot;)));
    int rows = 2500000;
    int columns = 3;

    double[][] arrayOfEarth = new double[rows][columns];
    while (input.hasNextLine()) {
        for (int a = 0; a &lt; arrayOfEarth.length; a++) {
            String[] line = input.nextLine().trim().split(&quot;/t &quot;);
            for (int b = 0; b &lt; line.length; b++) {
                arrayOfEarth[a][b] = Double.parseDouble(line[b]);
            }
        }
    }
    System.out.println(Arrays.deepToString(arrayOfEarth));
}

public static List&lt;Double&gt; CoordinatesAbove(double altitude) {
    List&lt;Double&gt; CoordinatesAbove = new ArrayList&lt;&gt;();
    for (int c = 0; c &lt; arrayOfEarth.length; c++) {
        CoordinatesAbove.add(arrayOfEarth[c][2]);
    }
    CoordinatesAbove.removeIf(d -&gt; d &lt; altitude);
    return CoordinatesAbove;
}

答案1

得分: 0

只需将其移到类级别并使其为静态(否则静态方法无法使用它)

private static double[][] arrayOfEarth;

public static void readDataArray(String filename) throws FileNotFoundException {
    Scanner input = new Scanner(new BufferedReader(new FileReader(filename)));
    int rows = 2500000;
    int columns = 3;

    arrayOfEarth = new double[rows][columns];
    while (input.hasNextLine()) {
        for (int a = 0; a < arrayOfEarth.length; a++) {
            String[] line = input.nextLine().trim().split("\t");
            for (int b = 0; b < line.length; b++) {
                arrayOfEarth[a][b] = Double.parseDouble(line[b]);
            }
        }
    }
    System.out.println(Arrays.deepToString(arrayOfEarth));
}

public static List<Double> CoordinatesAbove(double altitude) {
    List<Double> CoordinatesAbove = new ArrayList<>();
    for (int c = 0; c < arrayOfEarth.length; c++) {
        CoordinatesAbove.add(arrayOfEarth[c][2]);
    }
    CoordinatesAbove.removeIf(d -> d < altitude);
    return CoordinatesAbove;
}
英文:

Just move it to the class level and make it static (otherwise the static method can't use it)

private static double[][] arrayOfEarth;

public static void readDataArray(String filename) throws FileNotFoundException {
    Scanner input = new Scanner(new BufferedReader(new FileReader(&quot;sample.xyz&quot;)));
    int rows = 2500000;
    int columns = 3;

    arrayOfEarth = new double[rows][columns];
    while (input.hasNextLine()) {
        for (int a = 0; a &lt; arrayOfEarth.length; a++) {
            String[] line = input.nextLine().trim().split(&quot;/t &quot;);
            for (int b = 0; b &lt; line.length; b++) {
                arrayOfEarth[a][b] = Double.parseDouble(line[b]);
            }
        }
    }
    System.out.println(Arrays.deepToString(arrayOfEarth));
}

public static List&lt;Double&gt; CoordinatesAbove(double altitude) {
    List&lt;Double&gt; CoordinatesAbove = new ArrayList&lt;&gt;();
    for (int c = 0; c &lt; arrayOfEarth.length; c++) {
        CoordinatesAbove.add(arrayOfEarth[c][2]);
    }
    CoordinatesAbove.removeIf(d -&gt; d &lt; altitude);
    return CoordinatesAbove;
}

答案2

得分: 0

让你的readDataArray方法将值返回给调用者,并且让调用CoordinatesAbove方法的人将该值传递给它。

将值返回并将其作为参数接受的方式:

public static double[][] readDataArray(String filename) throws FileNotFoundException {
   ...
   System.out.println(Arrays.deepToString(arrayOfEarth));
   return arrayOfEarth;
}

public static List<Double> CoordinatesAbove(double altitude, double[][] arrayOfEarth) {
   ...
}

捕获readDataArray的返回值并将其传递给CoordinatesAbove的方式:

double[][] arrayOfEarth = readDataArray(...)
List<Double> list = CoordinatesAbove(altitude, arrayOfEarth);

通过这种方式,明确信息的流动,比在一个方法中将结果写入一个共同的全局变量,然后在另一个方法中读取该变量来隐藏流程要更好。

英文:

Make your readDataArray method return the value to the caller, and have whoever is calling the CoordinatesAbove method pass the value to it.

To return the value and to accept it as a parameter:

public static double[][] readDataArray(String filename) throws FileNotFoundException {
   ...
   System.out.println(Arrays.deepToString(arrayOfEarth));
   return arrayOfEarth;
}

public static List&lt;Double&gt; CoordinatesAbove(double altitude, double[][] arrayOfEarth) {
   ...
}

To capture the return value of readDataArray and to pass it to CoordinatesAbove:

double[][] arrayOfEarth = readDataArray(...)
List&lt;Double&gt; list = CoordinatesAbove(altitude, arrayOfEarth);

It is arguably better to make the flow of information explicit like this, than hiding the flow by writing the results to a common global variable in one method and reading the variable in the other.

huangapple
  • 本文由 发表于 2020年4月8日 20:27:57
  • 转载请务必保留本文链接:https://java.coder-hub.com/61100664.html
匿名

发表评论

匿名网友

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

确定