【问题标题】:Returning Multiple ArrayList<ArrayList<String>>返回多个 ArrayList<ArrayList<String>>
【发布时间】:2016-02-22 10:58:12
【问题描述】:

我有一个方法可以创建 4 个数组列表的数组列表,我正在尝试找出返回该数据的方法。我知道它不会让我做多个 return 语句,那么有没有办法将 ArrayLists 组合在一起返回?

public class sys {
public static void main(String[] args) 
{
    try 
    {
        FileInputStream file = new FileInputStream(new File("data/database.xlsx"));
        XSSFWorkbook mainDB = new XSSFWorkbook(file);

        int i = 0;
        do
        {
            ArrayList<ArrayList<String>> sheet1 = new ArrayList<ArrayList<String>>();
            ArrayList<ArrayList<String>> sheet2 = new ArrayList<ArrayList<String>>();
            ArrayList<ArrayList<String>> sheet3 = new ArrayList<ArrayList<String>>();
            ArrayList<ArrayList<String>> sheet4 = new ArrayList<ArrayList<String>>();
            ArrayList<String> cellArray = new ArrayList<String>();      
            XSSFSheet sheet = mainDB.getSheetAt(i);
            Iterator<Row> rowIterator = sheet.iterator();
            rowIterator.next();
            while(rowIterator.hasNext())
            {
                Row row = rowIterator.next();
                if(row.getRowNum()==0)
                {
                       continue; //skip the first row
                }
                else
                {
                    Iterator<Cell> cellIterator = row.cellIterator();
                    while(cellIterator.hasNext())
                    {
                        Cell cell = cellIterator.next();
                        //switch(cell.getCellType()) 
                        {
                            String c = "";
                            if(cell.getCellType() == Cell.CELL_TYPE_NUMERIC)
                            {
                                c = Integer.toString((int)(cell.getNumericCellValue()));
                            }
                            if(cell.getCellType() == Cell.CELL_TYPE_STRING)
                            {
                                c = cell.getStringCellValue();
                            }
                            cellArray.add(c);
                        }
                    }
                    if (i==0);
                    {
                        sheet1.add(cellArray);
                    }
                    if (i==1);
                    {
                        sheet2.add(cellArray);
                    }
                    if (i==2);
                    {
                        sheet3.add(cellArray);
                    }
                    if (i==3);
                    {
                        sheet4.add(cellArray);
                    }
                }
            }
            return sheet1;
            return sheet2;
            return sheet3;
            return sheet4;

            i++;

        } 
        while (i<4);
        file.close();


    }

    catch (FileNotFoundException error1) {
        error1.printStackTrace();
    } 

    catch (IOException error2) {
        error2.printStackTrace();
    }
}

}

【问题讨论】:

  • 返回HashMap of ArrayList?
  • ArrayList 是可变的。您可以将空的ArrayLists 传递给该方法,该方法可以填充它们。这取决于方法的作用。我们需要看一些代码。

标签: java multidimensional-array arraylist


【解决方案1】:

如果您有一定数量的 ArrayList,那么最有效的方法是创建一个 ArrayList 数组,如下所示:

ArrayList<ArrayList<String>>[] array = new ArrayList<ArrayList<String>>[] {arrayList1, arrayList2, arrayList3, arrayList4};

或如下:

ArrayList<ArrayList<String>>[] array = new ArrayList<ArrayList<String>>[4];
array[0] = arrayList1;
array[1] = arrayList2;
array[2] = arrayList3;
array[3] = arrayList4;

编辑: 正如 cmets 所指出的,上述实际上是不可能的!这是一个更新的解决方案:

    ArrayList<ArrayList<String>>[] array = (ArrayList<ArrayList<String>>[])  Array.newInstance(arrayList1.getClass(), arrayList1.size());

*假设 arrayList1 是 OP 提到的 ArrayLists 之一

【讨论】:

  • 无法创建通用数组。
  • 我以为 ArrayList 是字符串类型的?
  • 这里不涉及泛型
  • new ArrayList&lt;ArrayList&lt;String&gt;&gt;[] 无法编译。试试看。
  • 我同意,但为了提高效率,我会继续使用数组
【解决方案2】:
public class ArrayListUtils {

    public static void main(String[] args) {
        ArrayListUtils listUtils = new ArrayListUtils();

        ArrayList<String> one = new ArrayList<>();
        one.add("A");
        one.add("B");

        ArrayList<String> two = new ArrayList<>();
        two.add("AA");
        two.add("BA");

        ArrayList<String> three = new ArrayList<>();
        three.add("AAA");
        three.add("BAA");

        ArrayList<String> four = new ArrayList<>();
        four.add("AAAA");
        four.add("BAAA");

        //If you need ArrayList of four ArrayList<String> object
        ArrayList<ArrayList<String>> arrayList = listUtils.getArrayList(one, two, three, four);

        //If you need List of four ArrayList<String> object
        List<ArrayList<String>> list = listUtils.getList(one, two, three, four);

    }

    /*
     ArrayList<String>... strings :: means varargs 
     */
    public ArrayList<ArrayList<String>> getArrayList(ArrayList<String>... strings) {
        ArrayList<ArrayList<String>> list = new ArrayList<>();
        list.addAll(Arrays.asList(strings));
        return list;
    }

    public List<ArrayList<String>> getList(ArrayList<String>... strings) {
        return Arrays.asList(strings);
    }
}

【讨论】:

    猜你喜欢
    • 2013-07-02
    • 2018-05-16
    • 1970-01-01
    • 2012-05-27
    • 2013-07-06
    • 1970-01-01
    • 2016-11-04
    • 1970-01-01
    • 2015-08-06
    相关资源
    最近更新 更多