-8
public class TestUtil extends TestBase {

    TestUtil testUtil;
    static Workbook book;
    static Sheet sheet;

    public void switchToFrame() {
        driver.switchTo().frame("mainpanel");
    }

    public static String TESTDATA_SHEET_PATH = ".\\src\\main\\java\\com\\testData\\FreeCRMTestData.xlsx";

    public static Object[][] getTestData(String sheetName) {
        FileInputStream file = null;
        try {
            file = new FileInputStream(TESTDATA_SHEET_PATH);
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            book = WorkbookFactory.create(file);
        } catch (Exception e) {
            e.printStackTrace();
        }

        // Get the Sheet.
        sheet = book.getSheet(sheetName);

        //getLastRowNum() Gets the number last row on the sheet.
        //getLastCellNum() Gets the index of the last cell contained in this row PLUS ONE.
        Object[][] data = new Object[sheet.getLastRowNum()][sheet.getRow(0).getLastCellNum()];
        for (int i = 0; i < sheet.getLastRowNum(); i++) {
            for (int k = 0; k < sheet.getRow(0).getLastCellNum(); k++) {
                data[i][k] = sheet.getRow(i + 1).getCell(k).toString();
            }
        }
        return data;
    }

}

Description:- I want to read an Excel file from Java with X columns and Y rows. What does the statement Object[][] data = new Object [][] mean? Can anyone give me simple comments for the whole code snippet?

bsheps
  • 1,034
  • 11
  • 22
  • 1
    `int [ ] [ ]` is not an array of integers. It's an array of arrays of integers, also referred to as a 2-dimensional array. See [this question](https://stackoverflow.com/a/1200646/3230218) for info on creating arrays in Java. – Reinstate Monica Oct 14 '18 at 14:46
  • 1
    Please improve the quality of your question including 1) providing real compilable code, and 2) clarifying just exactly what it is you're asking and why -- what generated this question – Hovercraft Full Of Eels Oct 14 '18 at 14:51
  • 1
    You've been asked to supply real compilable code and to tell the details of your question, but have not yet done so, and this may have something to do with the down-votes. Please go through the [ask] to learn more about best practices when asking a question, and to help you improve this and future questions. – Hovercraft Full Of Eels Oct 14 '18 at 14:57
  • 1
    If you ***improve*** this question, this may happen, and usually the sooner the better, since some of the down-voters may have lost interest in the question by now. – Hovercraft Full Of Eels Oct 14 '18 at 15:00
  • The `Object[][] data` is a reference to an array of references to arrays of references. You can assign it with `= new Object[n][];` or `= new Object[n][m];` – Peter Lawrey Oct 14 '18 at 15:21

4 Answers4

1

In this statement

Object[][] data = new Object[sheet.getLastRowNum()][sheet.getRow(0).getLastCellNum()];

It is creating an array of references to arrays of references to Objects. The first array is for rows and the leaf arrays are for column values.

In reality, only String objects are added so I would write it like this.

int rows = sheet.getLastRowNum();
int columns = sheet.getRow(0).getLastCellNum();
String[][] data = new String[rows][columns];

If you want to see how this looks at runtime, I suggest stepping through the code in your debugger.

Peter Lawrey
  • 498,481
  • 72
  • 700
  • 1,075
0

*Edit: this answer was written before the question changed.

What does the statement Object[ ][ ] data = new Object [ ][ ] mean in Java?

It means a 2 dimensional array of objects. What is an object? An object is any non-primitive data type in java.

Note: you will need to specify the size when instantiating

Example:

String[][] foo = new String[2][3];

bsheps
  • 1,034
  • 11
  • 22
0

Object[][] data means an array of arrays of Objects.

For example:

Object[][] data = new Object[3][4];

constructs an array with 3 elements. Each of these 3 elements is an array with 4 Objects. However all these Objects are still null.

Thomas Fritsch
  • 7,861
  • 33
  • 31
  • 41
0
"Object[][] data = new Object[][]"

"Object[][]" is a 2 dimensional array of objects

"data" is the name of this array

"new Object[][]", here you are saying to your computer, that array have a structure of x rows and y columns to do an example:

"Object[][] data = new Object[5][4]" --> in this case you created an array with 5 rows, and 4 columns, in 2 dimensions would be like that:

[][][][]row number 1 with 5 columns

[][][][]row number 2 with 5 columns

[][][][]row number 3 with 5 columns

[][][][]row number 4 with 5 columns

[][][][]row number 5 with 5 columns

but your computer will see it more like that :

[][][][] [][][][] [][][][] [][][][] [][][][] 

and in all the box's could be an object

zubergu
  • 3,341
  • 3
  • 22
  • 34