McCurry0x77 0 Newbie Poster

I'm required to construct a three-dimensional array (a "universe"), but unlike traditional array navigation where you go to the end of the column, row, etc., increment and start at the beginning, I am required to move in reverse (like actually navigating a cube). The precise instructions:

- Move along a row (through the columns) until it comes to a side edge
- Move one layer deeper and then travel back along the row until it is back at a side edge
- Repeat these movements until the back edge is reached. Then move down to the next row.
- Move along a row (through the columns) until it comes to a side edge
- Move one layer forward and then travel back along the row until it is at a side edge
- Repeat the last 2 movements until the front edge is reached. Then move down to the next
row.
- Now repeat ALL of the above until the universe is exhausted

My code is as follows, though it may be hard to interpret. The loop structure follows the desired pattern perfectly until x=1, y=1, and z=0. What's happening here that causes this error? How can I fix it? Note: I cannot use any API classes.

public static void searchUniverse(int array[][][]){
		int columnInverse=0, depthInverse=0, rowInverse=0, row=0, column=0, depth=0;
		for (int x=0; x<array.length; x++){			//row 1 --> 1
			for (int y=0; y<array[x].length; y++){		//depth 0 --> 1
				depthInverse=array[x].length;
				if (x%2==0)
					depthInverse=2*y+1;
				for (int z=0; z<array[y].length; z++){	//column 2 --> 0
					columnInverse=array[y].length;
					if (y%2==0 && x%2==0)
						columnInverse=2*z+1;
					System.out.println(x+" Row\t\t"+(x+1)+"\n"+y+" Depth\t\t"+(depthInverse-y)+"\n"+z+" Column\t"+(columnInverse-z)+"\n");
				}
			}
		}
	}

Dani AI

Generated

— the crash at x=1, y=1, z=0 comes from two common mistakes in 3‑D Java loops: indexing the wrong subarray and off‑by‑one mirror math. Using array[y] inside the z‑loop (instead of array[x][y]) reads the wrong dimension and will blow up when the subarrays are ragged. And to flip an index you want (len - 1 - i), not len - i or ad‑hoc formulas like 2*i+1.

A clearer, easier-to-reason pattern: iterate rows in their natural order; decide the depth traversal direction from the row parity; for each depth step compute the actual depth index (forward or backward); toggle the column direction on every depth step and compute the actual column index the same way. This uses simple boolean flips and avoids tricky arithmetic.

Example implementation (safe for ragged arrays):

public static void searchUniverse(int[][][] array) {
    for (int x = 0; x < array.length; x++) {
        int depthCount = (array[x] == null) ? 0 : array[x].length;
        boolean depthForward = (x % 2 == 0);
        for (int dStep = 0; dStep < depthCount; dStep++) {
            int y = depthForward ? dStep : depthCount - 1 - dStep;
            if (array[x][y] == null) continue;
            int colCount = array[x][y].length;
            boolean colForward = (dStep % 2 == 0);
            for (int cStep = 0; cStep < colCount; cStep++) {
                int z = colForward ? cStep : colCount - 1 - cStep;
                System.out.printf("x=%d y=%d z=%d value=%d%n", x, y, z, array[x][y][z]);
            }
        }
    }
}

Troubleshooting tips: print array.length, array[x].length, and array[x][y].length to validate dimensions; check for null subarrays before indexing; and prefer the start/step/stop pattern above instead of inlined mirror arithmetic. This will fix the x=1,y=1,z=0 error and make the traversal logic much easier to follow.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.