返回多个ArrayList <arraylist <string>&gt;

时间:2015-11-20 03:30:08

标签: java multidimensional-array arraylist

我有一个方法,它已经制作了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();
    }
}

}

2 个答案:

答案 0 :(得分:0)

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

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;

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

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

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

答案 1 :(得分:0)

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