Undefined
Undefined

Reputation: 655

Understanding Three-Dimensional Arrays

I'm trying to wrap my head around three-dimensional arrays. I understand that they are arrays of two-dimensional arrays, but the book I'm reading said something that confuses me.

In an exercise for the book I'm reading, it asks me to make a three-dimensional array for a full-color image. It gives a small example saying this:

If we decide to choose a three-dimensional array, here's how the array might be declared:

int[][][] colorImage = new int[numRows][numColumns][3];

However, wouldn't it be more effective like this?

int[][][] colorImage = new int[3][numRows][numColumns];

Where 3 is the rgb values, 0 being red, 1 being green, and 2 being blue. With the latter, each two-dimensional array would be storing the color value of the row and column, right? I just want to make sure I understand how to effectively use a three-dimensional array.

Any help will be greatly appreciated, thanks.

Upvotes: 9

Views: 50632

Answers (3)

Mirko
Mirko

Reputation: 1552

I'm not sure if its a good idea to put everything in an 3dimensional array of int.

Your first mistake is the dataytpe: RGB is a int. But R is a byte, G is a byte, B is a byte too.. (Color.getXXX() delivers an int, I dont know why because its a byte 0-255)

You need an int because you want to address more than 256 cols&rows. (Thats okay). But i think that its much better to encapsulate the color information in a extra object. Perhaps a private datastructure like

class MyColor {

        public byte r, g, b;    //public for efficient access;
        public int  color;      //public for efficient access;

        public MyColor(final int rgb) {
            this(new Color(rgb));
        }

        public MyColor(final Color c) {
            this((byte) c.getRed(), (byte) c.getGreen(), (byte) c.getBlue(), c.getRGB());
        }

        public MyColor(final byte red, final byte green, final byte blue, final int c) {
            this.r = red;
            this.g = green;
            this.b = blue;
            this.color = c;
        }
    }

and put this in an 2dim array of MyColor[numRows][numColumns]

But if you make the class MyColor public to your whole app - i would change the design of the class to be more secure.

Upvotes: 0

Strelok
Strelok

Reputation: 51441

Order doesn't matter, and in fact the former form is more readable:

final const int RED = 0;
final const int GREEN = 1;
final const int BLUE = 2;

int[][][] colorImage = new int[numRows][numColumns][3];
//...

int x = getSomeX();
int y = getSomeY();

int redComponent = colorImage[x][y][RED];
int greenComponent = colorImage[x][y][GREEN];
int blueComponent = colorImage[x][y][BLUE];

Upvotes: 3

Tyson
Tyson

Reputation: 1715

The order shouldn't matter, so one isn't more effective than the other. The only thing that matters is that whatever accesses colorImage knows which dimension is used for what. Bit more context on multidimensional arrays here.

Upvotes: 2

Related Questions