简体   繁体   English

当大小为动态时,如何从方法返回二维数组?

[英]How to return 2d array from a method when it's size is dynamic?

Hi i have following piece of code, here i have to return data which store in array s[][] from this function, Please help how i can do it. 嗨,我有以下一段代码,在这里我必须从该函数返回存储在数组s[][]数据,请帮助我如何做到这一点。

 public static String[][] readxl(String filepath) throws FileNotFoundException, IOException
        {
            FileInputStream file = new FileInputStream(new File(filepath));
            HSSFWorkbook workbook = new HSSFWorkbook(file);
            HSSFSheet sheet = workbook.getSheetAt(0);
            int rowcount = sheet.getPhysicalNumberOfRows();
            Row row1 = sheet.getRow(0);
            int colcount=row1.getPhysicalNumberOfCells();
            String[][]s = new String[rowcount][colcount];
            for(int i=0;i<sheet.getPhysicalNumberOfRows();i++)
            {
                Row row = sheet.getRow(i);
                for(int j=0; j<row.getPhysicalNumberOfCells();j++)
                {
                    Cell c = sheet.getRow(i).getCell(j);
                    int celltype = c.getCellType();
                    if(celltype==1)
                    {
                        s[i][j] =c.getStringCellValue();               
                    }
                    if(celltype==0)
                    {   
                        s[i][j] =String.valueOf((int)c.getNumericCellValue()); 
                    }

                }
            }

You can directly use 您可以直接使用

return s;

There is no problem in returning an array even if size is dynamic. 即使大小是动态的,返回数组也没有问题。

Good luck. 祝好运。

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

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