返回ArrayList和使用不同的类 [英] Returning arraylist and using in different class

查看:146
本文介绍了返回ArrayList和使用不同的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从这个方法返回一个ArrayList

I am trying to return an arraylist from this method

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

正如我希望在另一个类中使用它。第二类目前看起来是这样的:

As I wish to use it in another class. The second class currently looks like this :

public class mean extends reader{

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

}

}

有人能告诉我,我究竟做错了什么?每当我试图返回行,我得到一个错误信息说无效方法不能返回值。

Could someone tell me what am I doing wrong? Whenever I try to return rows I get an error message saying that void methods cannot return a value.

推荐答案

您没有创建在对象与从返回的东西的方法。创建类似下面的方法签名:

You didn't create a method in your reader object with which to return something from. Create a method signature like the following:

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

这篇关于返回ArrayList和使用不同的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆