0

I want to create a ragged 3d array as followes in java.

Terminology: A 2D array is said to consist of rows and columns. A 3D array is said to consist of slabs, where each slab consists of a 2D array.

The first slab has three rows, the second slab five rows, and the third slab seven rows (i.e., if s denotes the slab, the number of rows in the sth slab is 3+2*s). Within the sth slab, the jth row should have s+j+1 columns

My approach was,

int[][][] mat3d = new int[3][][];
mat3d[0] = new int[3][];
mat3d[0] = new int[5][];

But this gives a compile error. Can anyone please help me to do this. I'm in a real hurry.

4
  • 1
    You'll get answers faster if you actually tell us the error, rather than making us guess. By the way, it compiles fine when I type it, assuming it's inside a method. Commented Dec 5, 2014 at 8:50
  • @DavidWallace Thanks for pointing me in the correct direction. My hurry has pushed me to write this outside a method which is actually silly. Thanks again. Commented Dec 5, 2014 at 8:53
  • @DavidWallace Any idea what should I do to this question? Delete or Edit? Commented Dec 5, 2014 at 8:55
  • You should post your solution to it as an answer. Commented Dec 5, 2014 at 8:58

1 Answer 1

2

Error was not due to the code fragment in the question. Compilation failed as the code was not written inside a method. Writing the code with in a method fixes the problem.

public static void main(String args[]){
    int[][][] mat3d = new int[3][][];
    mat3d[0] = new int[3][];
    mat3d[0] = new int[5][];
}

This compiles fine.

Sign up to request clarification or add additional context in comments.

1 Comment

Until I realized you had answered your own question, I was like "Why the hell would someone answer something that the poster is obviously not that ignorant to not do". Heh. Good to see people solving their own issues :).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.