英文:
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
List
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("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<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;
}
答案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("sample.xyz")));
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;
}
答案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<Double> CoordinatesAbove(double altitude, double[][] arrayOfEarth) {
...
}
To capture the return value of readDataArray
and to pass it to CoordinatesAbove
:
double[][] arrayOfEarth = readDataArray(...)
List<Double> 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.
专注分享java语言的经验与见解,让所有开发者获益!
评论