10.12.2012 Views

The Java Language Specification, Third Edition

The Java Language Specification, Third Edition

The Java Language Specification, Third Edition

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

EXPRESSIONS Example: Array Creation Evaluation Order 15.10.2<br />

and:<br />

Age[][][][][] Aquarius = new Age[6][10][8][12][];<br />

is equivalent to:<br />

Age[][][][][] Aquarius = new Age[6][][][][];<br />

for (int d1 = 0; d1 < Aquarius.length; d1++) {<br />

Aquarius[d1] = new Age[10][][][];<br />

for (int d2 = 0; d2 < Aquarius[d1].length; d2++) {<br />

Aquarius[d1][d2] = new Age[8][][];<br />

for (int d3 = 0; d3 < Aquarius[d1][d2].length; d3++) {<br />

Aquarius[d1][d2][d3] = new Age[12][];<br />

}<br />

}<br />

}<br />

with d, d1, d2 and d3 replaced by names that are not already locally declared.<br />

Thus, a single new expression actually creates one array of length 6, 6 arrays of<br />

length 10, 6× 10 = 60 arrays of length 8, and 6 × 10 × 8 =<br />

480 arrays of length<br />

12. This example leaves the fifth dimension, which would be arrays containing the<br />

actual array elements (references to Age objects), initialized only to null references.<br />

<strong>The</strong>se arrays can be filled in later by other code, such as:<br />

Age[] Hair = { new Age("quartz"), new Age("topaz") };<br />

Aquarius[1][9][6][9] = Hair;<br />

A multidimensional array need not have arrays of the same length at each<br />

level.<br />

Thus, a triangular matrix may be created by:<br />

float triang[][] = new float[100][];<br />

for (int i = 0; i < triang.length; i++)<br />

triang[i] = new float[i+1];<br />

15.10.2 Example: Array Creation Evaluation Order<br />

DRAFT<br />

In an array creation expression (§15.10), there may be one or more dimension<br />

expressions, each within brackets. Each dimension expression is fully evaluated<br />

before any part of any dimension expression to its right.<br />

Thus:<br />

class Test {<br />

public static void main(String[] args) {<br />

int i = 4;<br />

int ia[][] = new int[i][i=3];<br />

System.out.println(<br />

"[" + ia.length + "," + ia[0].length + "]");<br />

}<br />

}<br />

433

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!