0

我正在尝试从此方法返回一个数组列表

 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;
}
}

因为我希望在另一个课程中使用它。第二类目前看起来像这样:

public class mean extends reader{

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

}

}

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

4

1 回答 1

2

你没有在你的reader对象中创建一个方法来返回一些东西。创建一个方法签名,如下所示:

public ArrayList<String[]> getRows() {
    // Rest of your code here.
}
于 2012-05-21T22:40:20.980 回答