user7212219
user7212219

Reputation: 1

Multi-Dimensional array in Linked List not working?

So I have this code :

    public static void main (String[] args) throws IOException
{
    Queue popcorn = new LinkedList();
    BufferedReader in = null;
    int j = 0;
    try {
        File file2 = new File("Events.txt");
        in = new BufferedReader(new FileReader(file2));

        String str;
        String [][] process = new String[][];
        while ((str = in.readLine()) != null) {
            String[] arr = str.split(" ");
            for(int i=0 ; i<str.length() ; i++){
            process[j][i] = in.readLine();
        }
            j++;
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

} 

It doesn't work. It throws "Variable must provide either dimension expressions or an array initializer"

I am trying to model it after this webpage answer " http://www.chegg.com/homework-help/questions-and-answers/hired-biggy-s-popcorn-handle-popcorn-orders-store-write-java-console-application-reads-dat-q7220573 " which i am PRETTY SURE does not work. Anyway this linked list doesn't seem to be working out. Can someone point me in the right direction as far as my String[][] process declaration is concerned?

Upvotes: 0

Views: 67

Answers (1)

John Mendoza
John Mendoza

Reputation: 91

You can't just initialize an array with no dimension parameters. For example, this is invalid:

int[] array = new int[];

It needs to be initialized like this:

int[] array = new int[10];

Or simply:

int[] array;
// ... //
array = new int[10];

It's the same thing with multi-dimensional arrays. To make an array containing 3 arrays of size 5, you would put:

int[][] array = new int[3][5];

However, with 2D arrays, you may also put:

int[][] array = new int[3][];
array[0] = new int[5];
array[1] = new int[7];
// ... //

The point is, you need to define how many other arrays will be in your base array, and may also optionally define the size of all of the arrays or simply add them later. In this case, you'll need to change

String [][] process = new String[][];

to something like

String [][] process = new String[x][y];

Upvotes: 3

Related Questions