简体   繁体   中英

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.

 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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