How to declare and initialize two-dimensional arrays?

5

I have to save the row, column and contents of a worksheet. For this I created a two-dimensional matrix. My difficulty is how do I initialize it? I believe the statement is no longer correct because I get ArrayIndexOutOfBoundsException . In this situation, array is the appropriate data structure?

if ("Veículo".equals(cell.getStringCellValue())) {
                            String[][] referencia = new String[][];
                            for (int i = cell.getColumnIndex(); i < 4; i++) {
                                referencia[cell.getRow().getRowNum()][cell.getColumnIndex()] = cell.getStringCellValue();
                            }
                        }
    
asked by anonymous 15.04.2015 / 14:59

1 answer

4

To declare and initialize the array correctly you need to know how many rows and how many columns it will have, and then initialize the array as follows:

String[][] referencia = new String[quantidadeLinhas][quantidadecolunas];

where

String[][] referencia = new String[10][4];

results in

String[][] referencia = {
    {null,null,null,null},
    {null,null,null,null},
    {null,null,null,null},
    {null,null,null,null},
    {null,null,null,null},
    {null,null,null,null},
    {null,null,null,null},
    {null,null,null,null},
    {null,null,null,null},
    {null,null,null,null}
}
    
15.04.2015 / 15:26