了解三维数组 [英] 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];
其中3是rgb值,0是红色,1是绿色,2是蓝色.对于后者,每个二维数组将存储行和列的颜色值,对吗?我只想确保我了解如何有效地使用三维数组.
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.
非常感谢您的帮助.
推荐答案
顺序无关紧要,实际上前一种形式更易于阅读:
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];
这篇关于了解三维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!