简体   繁体   English

返回多个ArrayList <ArrayList<String> &gt;

[英]Returning Multiple ArrayList<ArrayList<String>>

I have a method that has made 4 Array lists of array lists and I am trying to figure out out to return that data. 我有一个方法,使4个数组列表的数组列表,我试图找出要返回的数据。 I know it will not let me do more than one return statement, so is there a way to combine the ArrayLists together to return? 我知道它不会让我做不止一个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();
    }
}

} }

If you have a set number of ArrayLists, then the most efficient way to do it would be to create an array of ArraysLists, as such: 如果您有一定数量的ArrayLists,那么最有效的方法是创建一个ArraysLists数组,如下所示:

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

or as follows: 或如下:

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

EDIT: As comments pointed out, the above is actually not possible! 编辑:正如评论所指出,上述实际上是不可能的! Here's an updated solution: 这是更新的解决方案:

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

*assuming arrayList1 is one of the OP's mentioned ArrayLists *假设arrayList1是OP提到的ArrayLists之一

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM