gpt4 book ai didi

java - 返回数组列表并在不同的类中使用

转载 作者:行者123 更新时间:2023-12-02 07:46:58 26 4
gpt4 key购买 nike

我试图从此方法返回一个数组列表

 import java.io.*;
import java.util.*;
/** The class reader read the file 2RainfallDataLanc.txt and stores is as an ArrayList.
* It also parses the variables within the file by white spaces, making variables accessible.
*
*/
public class reader {

public ArrayList<String[]> getRows(){

try {
BufferedReader reader = new BufferedReader(new FileReader("2RainfallDataLanc.txt"));
String line = null;
ArrayList<String[]> rows = new ArrayList<String[]>();

while((line=reader.readLine())!=null)
{String[] row = line.split("\\s+");
rows.add(row);

}
for (String[] row : rows) {
System.out.println(Arrays.toString(row));
return rows;
}

} catch (IOException e) {
}
return null;
}
}

因为我想在另一个类(class)中使用它。第二个类目前看起来像这样:

public class mean extends reader{

public static void main(String[] args) {
reader newarray = new reader();

}

}

有人可以告诉我我做错了什么吗?每当我尝试返回行时,我都会收到一条错误消息,指出 void 方法无法返回值。

最佳答案

您没有在 reader 对象中创建用于返回内容的方法。创建如下方法签名:

public ArrayList<String[]> getRows() {
// Rest of your code here.
}

关于java - 返回数组列表并在不同的类中使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10693575/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com