# error using 3d array

Anshul Singhal

Greenhorn

Posts: 18

Greg Brannon

Bartender

Posts: 563

posted 5 years ago

It means that you've exceeded the size of your array somewhere, probably in that for loop, but I'm too lazy to count all of your initializations to figure out which dimension was violated. Your loop expects 15 i elements, 22 j, and 2 k for the array[i][j][k]. So you should ensure that all of your initializations include at least 15 i elements (0 - 14), 22 j (0 - 21), and 2 k (0 - 1). That would be 15 x 22 x 2 = 660 elements. Phew!

I see your first initialization has 22 elements. Shouldn't that be 15? Did you swap the i and j dimensions? It's hard to visualize and work with a 3(or more)-dimensional array, so I appreciate the challenge you have to keep it all straight.

I see your first initialization has 22 elements. Shouldn't that be 15? Did you swap the i and j dimensions? It's hard to visualize and work with a 3(or more)-dimensional array, so I appreciate the challenge you have to keep it all straight.

Always learning Java, currently using Eclipse on Fedora.

Linux user#: 501795

Anshul Singhal

Greenhorn

Posts: 18

posted 5 years ago

if you format your array a little better:

you can see what you have is an array of 15 elements (the i dimension).

Each of those elements are arrays of TWO elements (the j dimention), each of which contain arrays of 22 (k)elements. Your for loops should be like this:

you can see what you have is an array of 15 elements (the i dimension).

Each of those elements are arrays of TWO elements (the j dimention), each of which contain arrays of 22 (k)elements. Your for loops should be like this:

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors

Greg Brannon

Bartender

Posts: 563

posted 5 years ago

Sorry, thought about it some more, but I'm glad my question (however wrong) got you to think about it and try something.

A one-dimensional array, array1D[5], has 5 elements that we visualize in a row, or as a single row of 5 columns:

A two-dimensional array, array2D[4][5], we visualize in rows and columns. In this case, 4 rows of 5 columns:

Notice that the last dimension remains the number of columns.

A three-dimensional array, array3D[2][4][5] can be thought of as a 4 x 5 array 2 deep or two 4 x 5 arrays:

Notice that the last dimension describes the number of columns.

So, what is your array? Since the one line I counted had 22 members, there must be 22 columns, so that would be the last dimension. I'm not sure how the rest of it is organized.

I haven't given you a complete answer to your question, but I've certainly given you enough to complete the answer yourself.

Is 4D next?

A one-dimensional array, array1D[5], has 5 elements that we visualize in a row, or as a single row of 5 columns:

A two-dimensional array, array2D[4][5], we visualize in rows and columns. In this case, 4 rows of 5 columns:

Notice that the last dimension remains the number of columns.

A three-dimensional array, array3D[2][4][5] can be thought of as a 4 x 5 array 2 deep or two 4 x 5 arrays:

Notice that the last dimension describes the number of columns.

So, what is your array? Since the one line I counted had 22 members, there must be 22 columns, so that would be the last dimension. I'm not sure how the rest of it is organized.

I haven't given you a complete answer to your question, but I've certainly given you enough to complete the answer yourself.

Is 4D next?

Always learning Java, currently using Eclipse on Fedora.

Linux user#: 501795

Campbell Ritchie

Sheriff

Posts: 48402

56