您的位置:首页 > 编程语言 > Java开发

《Java 编程技巧1001条》 第389条: 了解不平衡数组

2017-12-22 09:10 393 查看



《Java 编程技巧1001条》第10章 用数组存储数据 第389条 了解不平衡数组 
 

389Understanding Unbalanced Arrays
389 了解非平衡数组

So far, thetwo-dimensional arrays that you have created have been rectangular. Rectangulararrays are like tables. All of the rows are of the same length, and all of thecolumns are of the same length. Javas multidimensional arrays are actuallyarrays of arrays. You do not have to have Eeach dimension does not have to beof a fixed length,. but dimensions must be specified left to right. Forexample, the following declaration is legal because the elements are specifiedfrom left to right:

在此之前,你建立过的二维数组是矩形数组. 矩形数组象表一样. 所有的行有同样的长度, 所有的列也有同样的长度. Java的多维数组实际也是数组的数组. 你并不一定要求每一维数组的大小都是一定的. 但维数必须从左到右依次说明. 例如,以下的声明是合法的,因元素的数目是从左到右说明的: /*legal dimension specification */boolean legal_ex[][] = new boolean[2][];
 
On theother hand, this array declaration is illegal because it specifies elementsfrom right to left:
相反,以下的声明是不合法的,因元素的数目是从右到左说明的:/* illegal dimension specification */boolean illegal_ex[][] = new boolean[][5];
 
Consider anexample of a two-dimensional unbalanced array. For example, sSuppose, forexample,d you use a two-dimensional array to keep track of a set of bowlingpins. Each row has a different number of pins. So, the size of the array foreach row is different, and the array is triangular instead of rectangular. Tocreate a triangular array like this, you first must declare it. For a set ofbowling pins, Tthe first dimension will be 4, representing the number of rows:.The second dimension will be unspecified. The following code demonstrates this:
考虑一个二维不平衡数组的例子. 例如,假如你要使用一个二维数组来保存一组滚轴 (bowling pins)的轨迹(track). 而每一行有不同的轴的数目. 这样,为每一行设置的数组大小是不一样的,这时的数组形状就要用三角形来代替长方形. 要建立这种样子的三角形数组,你首先要说明它. 对于一组滚轴,第一种尺寸是4,它表示行号. 而第二个尺寸将不指定.以下程序说明了这一情况:
 /* declare only the first dimension */boolean down_pins[][] = new boolean[4][];
 
Note: Youshould not specify a dimension on the right and leave a dimension on the leftunspecified. This will lead to a compile error.
注意: 你不应该指定右边的维数大小而不先给出左边的维数大小, 这会导致一个编译错误.
 
After youdeclare the array, you then create an array for each row. The following codedemonstrates how to create a triangular array:
当你声明了这个数组后, 你就可以为每一行创建一个数组. 以下语句说明怎样建立一个三角形的数组:public class arrayPins{    static public void main(String args)     {       /* declare only the first dimension */       boolean pins_fell [][] = newboolean[4][];        /* create the second dimension */       for (int i=0; i< pins_fell.length;i++)         {            pins_fell [i] = new boolean[i + 1];         }        /* print the contents of the array tothe screen */       for (int i=0; i< pins_fell.length;i++)         {           for (int k=0; k<pins_fell[i].length; k++)             {               System.out.print(pins_fell[i][k]+ " ");             }           System.out.println("");         }     }}
As you cansee, the program allocates columns for each row of the array. Later, theprogram displays the contents of the two-dimensional array. When you compileand execute this program, your screen will display the following output:
你可以看出,程序为数组的每一行分配一个列. 然后, 程序显示二维数组的内容. 当你编译和执行这一程序时,你的屏幕上将出现以下的输出结果:falsefalse falsefalse false falsefalse false falsefalse
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: