【问题标题】:How return a multidimensional arrayList in java?如何在java中返回多维arrayList?
【发布时间】:2018-10-20 21:10:40
【问题描述】:

我想创建一个多维数组并将其作为方法中的参数传递,然后用元素填充 arrayList 并返回新版本的 arrayList 以便能够在不同的类中使用该数组,但我得到 java.lang .NoSuchMethodError:我认为问题在于我返回数组的方式。我搜索但找不到。我怎样才能正确地做到这一点?

这是我的代码;

public test{

 public static List<List<String>> 2Darray=new ArrayList<List<String>>(); // TE ERROR IN THIS LINE


 public List<List<String>> fillArray(List<List<String>> array){

BufferedReader in = null;
            ArrayList<String> row = new ArrayList<String>();
            try {
                in = new BufferedReader(new FileReader("sampleFile.txt"));
                String read = null;
                    while ((read = in.readLine()) != null) {
                        String[] splited = read.split("\\s+");                       
                        for(int i=0; i<splited.length ; i++){                           
                            row.add(splited[i]);
                        }                        
                        array.add(row);
                    }                         

                } catch (IOException e) {
                System.out.println("There was a problem: " + e);
                e.printStackTrace();
            } finally {
                try {
                    in.close();
                } catch (IOException e) {
                }
      return array;
}

【问题讨论】:

  • 请包含完整的堆栈跟踪并告诉我们哪一行抛出异常

标签: java arraylist multidimensional-array return return-type


【解决方案1】:

稍加修改(只是让它编译)会导致这似乎没有问题。也许您的问题在其他地方。

public List<List<String>> fillArray(List<List<String>> array) {

    BufferedReader in = null;
    ArrayList<String> row = new ArrayList<String>();
    try {
        in = new BufferedReader(new FileReader("sampleFile.txt"));
        String read = null;
        while ((read = in.readLine()) != null) {
            String[] splited = read.split("\\s+");
            for (int i = 0; i < splited.length; i++) {
                row.add(splited[i]);
            }
            array.add(row);
        }

    } catch (IOException e) {
        System.out.println("There was a problem: " + e);
        e.printStackTrace();
    } finally {
        try {
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return array;
}

顺便说一句:你真的应该使用try with resources - 它更清晰。

【讨论】:

    【解决方案2】:

    稍微修改您的代码,使其能够编译,并将从文本文件中读取替换为读取字符串。有几个问题,但它似乎工作。试试看吧。

    我注意到的主要问题是大括号不匹配,以及以数字开头的变量名。

    import java.util.*;
    import java.io.*;
    
    public class Main {
      public static List<List<String>> array2D = new ArrayList<List<String>>();
    
      public List<List<String>> fillArray(List<List<String>> array) {
    
        BufferedReader in = null;
        ArrayList<String> row = new ArrayList<String>();
        try {
          String str = "Some test text";
          InputStream is = new ByteArrayInputStream(str.getBytes());
          //in = new BufferedReader(new FileReader("sampleFile.txt"));
          in = new BufferedReader(new InputStreamReader(is));
          String read = null;
          while ((read = in.readLine()) != null) {
            String[] splited = read.split("\\s+");                       
            for(int i=0; i<splited.length ; i++) {                           
                row.add(splited[i]);
            }                        
            array.add(row);
          }                         
    
        } 
        catch (IOException e) {
          System.out.println("There was a problem: " + e);
          e.printStackTrace();
        }
        finally {
          try {
            in.close();
          } 
          catch (IOException e) {
          }
        }
        return array;
      }
    
      public static void main(String[] args) {
        Main main = new Main();
        List<List<String>> test = main.fillArray(array2D);
    
        for(int i  = 0; i < test.size(); i++) {
          for(int j = 0; j < test.get(i).size(); j++) {
            System.out.println(test.get(i).get(j));
          }
        }
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-23
      • 2023-03-10
      • 1970-01-01
      • 2019-09-26
      • 2012-05-27
      • 2013-09-21
      • 2019-03-05
      • 1970-01-01
      相关资源
      最近更新 更多