Understanding 3D Arrays

I am trying to wrap my head around three-dimensional arrays. I understand that these are arrays of two-dimensional arrays, but the book I'm reading says what confuses me.

In the exercise for the book I'm reading, I am asked to create a three-dimensional array for a full-color image. This gives a small example:

If we decide to select a three-dimensional array, here is how an array can be declared:

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

However, it would not be more effective, how is it?

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

Where 3 is the rgb value, 0 is red, 1 is green, and 2 is blue. With the latter, each two-dimensional array will store the color value of the row and column, right? I just want to make sure that I understand how to use a three-dimensional array efficiently.

Any help would be greatly appreciated, thanks.

+5
3

, :

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];
+1

, . , , , colorImage , . .

+1

, int.

- dataytpe: RGB - int. R , G , B .. (Color.getXXX() int, , , 0-255)

int, 256 . ( ). , . , ,

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;
        }
    }

2dim MyColor[numRows][numColumns]

But if you make the MyColor class public for your entire application - I would change the class design to a more secure one.

0
source

All Articles