📄 tij0053.html
字号:
<font color="#990000"><PRE><font color="#009900">//: ArrayInit.java</font>
<font color="#009900">// Array initialization</font>
<font color="#0000ff">public</font> <font color="#0000ff">class</font> ArrayInit {
<font color="#0000ff">public</font> <font color="#0000ff">static</font> <font color="#0000ff">void</font> main(String[] args) {
Integer[] a = {
<font color="#0000ff">new</font> Integer(1),
<font color="#0000ff">new</font> Integer(2),
<font color="#0000ff">new</font> Integer(3),
};
<font color="#009900">// Java 1.1 only:</font>
Integer[] b = <font color="#0000ff">new</font> Integer[] {
<font color="#0000ff">new</font> Integer(1),
<font color="#0000ff">new</font> Integer(2),
<font color="#0000ff">new</font> Integer(3),
};
}
} <font color="#009900">///:~ </PRE></font></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">This
is useful at times, but it’s more limited since the size of the array is
determined at compile time. The final comma in the list of initializers is
optional. (This feature makes for easier maintenance of long lists.)
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
second form of array initialization, added in Java 1.1<A NAME="Index339"></A>,
provides a convenient syntax to create and call methods that can produce the
same effect as C’s <A NAME="Index340"></A><A NAME="Index341"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>variable
argument lists
</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
(known as “varargs” in C). These included, if you choose, unknown
quantity of arguments as well as unknown type. Since all classes are ultimately
inherited from the common root class
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Object</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
you can create a method that takes an array of
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Object</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and call it like this:
</FONT><P></DIV>
<font color="#990000"><PRE><font color="#009900">//: VarArgs.java</font>
<font color="#009900">// Using the Java 1.1 array syntax to create</font>
<font color="#009900">// variable argument lists</font>
<font color="#0000ff">class</font> A { <font color="#0000ff">int</font> i; }
<font color="#0000ff">public</font> <font color="#0000ff">class</font> VarArgs {
<font color="#0000ff">static</font> <font color="#0000ff">void</font> f(Object[] x) {
<font color="#0000ff">for</font>(<font color="#0000ff">int</font> i = 0; i < x.length; i++)
System.out.println(x[i]);
}
<font color="#0000ff">public</font> <font color="#0000ff">static</font> <font color="#0000ff">void</font> main(String[] args) {
f(<font color="#0000ff">new</font> Object[] {
<font color="#0000ff">new</font> Integer(47), <font color="#0000ff">new</font> VarArgs(),
<font color="#0000ff">new</font> Float(3.14), <font color="#0000ff">new</font> Double(11.11) });
f(<font color="#0000ff">new</font> Object[] {"one", "two", "three" });
f(<font color="#0000ff">new</font> Object[] {<font color="#0000ff">new</font> A(), <font color="#0000ff">new</font> A(), <font color="#0000ff">new</font> A()});
}
} <font color="#009900">///:~ </PRE></font></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">At
this point, there’s not much you can do with these unknown objects, and
this program uses the automatic
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>String</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
conversion to do something useful with each
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Object</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
In Chapter 11 (run-time type identification or RTTI) you’ll learn how to
discover the exact type of such objects so that you can do something more
interesting with them.
</FONT><a name="_Toc408018490"></a><P></DIV>
<A NAME="Heading158"></A><H3 ALIGN=LEFT>
Multidimensional
arrays
</H3>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Java
allows you to easily create <A NAME="Index342"></A><A NAME="Index343"></A>multidimensional
arrays:
</FONT><P></DIV>
<font color="#990000"><PRE><font color="#009900">//: MultiDimArray.java</font>
<font color="#009900">// Creating multidimensional arrays.</font>
<font color="#0000ff">import</font> java.util.*;
<font color="#0000ff">public</font> <font color="#0000ff">class</font> MultiDimArray {
<font color="#0000ff">static</font> Random rand = <font color="#0000ff">new</font> Random();
<font color="#0000ff">static</font> <font color="#0000ff">int</font> pRand(<font color="#0000ff">int</font> mod) {
<font color="#0000ff">return</font> Math.abs(rand.nextInt()) % mod;
}
<font color="#0000ff">public</font> <font color="#0000ff">static</font> <font color="#0000ff">void</font> main(String[] args) {
<font color="#0000ff">int</font>[][] a1 = {
{ 1, 2, 3, },
{ 4, 5, 6, },
};
<font color="#0000ff">for</font>(<font color="#0000ff">int</font> i = 0; i < a1.length; i++)
<font color="#0000ff">for</font>(<font color="#0000ff">int</font> j = 0; j < a1[i].length; j++)
prt("a1[" + i + "][" + j +
"] = " + a1[i][j]);
<font color="#009900">// 3-D array with fixed length:</font>
<font color="#0000ff">int</font>[][][] a2 = <font color="#0000ff">new</font> <font color="#0000ff">int</font>[2][2][4];
<font color="#0000ff">for</font>(<font color="#0000ff">int</font> i = 0; i < a2.length; i++)
<font color="#0000ff">for</font>(<font color="#0000ff">int</font> j = 0; j < a2[i].length; j++)
<font color="#0000ff">for</font>(<font color="#0000ff">int</font> k = 0; k < a2[i][j].length;
k++)
prt("a2[" + i + "][" +
j + "][" + k +
"] = " + a2[i][j][k]);
<font color="#009900">// 3-D array with varied-length vectors:</font>
<font color="#0000ff">int</font>[][][] a3 = <font color="#0000ff">new</font> <font color="#0000ff">int</font>[pRand(7)][][];
<font color="#0000ff">for</font>(<font color="#0000ff">int</font> i = 0; i < a3.length; i++) {
a3[i] = <font color="#0000ff">new</font> <font color="#0000ff">int</font>[pRand(5)][];
<font color="#0000ff">for</font>(<font color="#0000ff">int</font> j = 0; j < a3[i].length; j++)
a3[i][j] = <font color="#0000ff">new</font> <font color="#0000ff">int</font>[pRand(5)];
}
<font color="#0000ff">for</font>(<font color="#0000ff">int</font> i = 0; i < a3.length; i++)
<font color="#0000ff">for</font>(<font color="#0000ff">int</font> j = 0; j < a3[i].length; j++)
<font color="#0000ff">for</font>(<font color="#0000ff">int</font> k = 0; k < a3[i][j].length;
k++)
prt("a3[" + i + "][" +
j + "][" + k +
"] = " + a3[i][j][k]);
<font color="#009900">// Array of non-primitive objects:</font>
Integer[][] a4 = {
{ <font color="#0000ff">new</font> Integer(1), <font color="#0000ff">new</font> Integer(2)},
{ <font color="#0000ff">new</font> Integer(3), <font color="#0000ff">new</font> Integer(4)},
{ <font color="#0000ff">new</font> Integer(5), <font color="#0000ff">new</font> Integer(6)},
};
<font color="#0000ff">for</font>(<font color="#0000ff">int</font> i = 0; i < a4.length; i++)
<font color="#0000ff">for</font>(<font color="#0000ff">int</font> j = 0; j < a4[i].length; j++)
prt("a4[" + i + "][" + j +
"] = " + a4[i][j]);
Integer[][] a5;
a5 = <font color="#0000ff">new</font> Integer[3][];
<font color="#0000ff">for</font>(<font color="#0000ff">int</font> i = 0; i < a5.length; i++) {
a5[i] = <font color="#0000ff">new</font> Integer[3];
<font color="#0000ff">for</font>(<font color="#0000ff">int</font> j = 0; j < a5[i].length; j++)
a5[i][j] = <font color="#0000ff">new</font> Integer(i*j);
}
<font color="#0000ff">for</font>(<font color="#0000ff">int</font> i = 0; i < a5.length; i++)
<font color="#0000ff">for</font>(<font color="#0000ff">int</font> j = 0; j < a5[i].length; j++)
prt("a5[" + i + "][" + j +
"] = " + a5[i][j]);
}
<font color="#0000ff">static</font> <font color="#0000ff">void</font> prt(String s) {
System.out.println(s);
}
} <font color="#009900">///:~ </PRE></font></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
code used for printing uses
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>length</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
so that it doesn’t depend on fixed array sizes.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
first example shows a multidimensional array of primitives. You delimit each
vector in the array with curly braces:
</FONT><P></DIV>
<font color="#990000"><PRE> <font color="#0000ff">int</font>[][] a1 = {
{ 1, 2, 3, },
{ 4, 5, 6, },
}; </PRE></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Each
set of square brackets moves you into the next level of the array.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
second example shows a three-dimensional array allocated with
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>new</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
Here, the whole array is allocated at once:
</FONT><P></DIV><DIV ALIGN=LEFT><TT><FONT FACE="Courier New" SIZE=3 COLOR="Black">int[][][]
a2 = new int[2][2][4];
</FONT></TT><P></DIV><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">But
the third example shows that each vector in the arrays that make up the matrix
can be of any length:
</FONT><P></DIV>
<font color="#990000"><PRE> <font color="#0000ff">int</font>[][][] a3 = <font color="#0000ff">new</font> <font color="#0000ff">int</font>[pRand(7)][][];
<font color="#0000ff">for</font>(<font color="#0000ff">int</font> i = 0; i < a3.length; i++) {
a3[i] = <font color="#0000ff">new</font> <font color="#0000ff">int</font>[pRand(5)][];
<font color="#0000ff">for</font>(<font color="#0000ff">int</font> j = 0; j < a3[i].length; j++)
a3[i][j] = <font color="#0000ff">new</font> <font color="#0000ff">int</font>[pRand(5)];
} </PRE></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
first
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>new</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
creates an array with a random-length first element and the rest undetermined.
The second
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>new</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
inside the for loop fills out the elements but leaves the third index
undetermined until you hit the third
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>new</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">You
will see from the output that array values are automatically initialized to
zero if you don’t give them an explicit initialization value.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">You
can deal with arrays of non-primitive objects in a similar fashion, which is
shown in the fourth example, demonstrating the ability to collect many
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>new</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
expressions with curly braces:
</FONT><P></DIV>
<font color="#990000"><PRE> Integer[][] a4 = {
{ <font color="#0000ff">new</font> Integer(1), <font color="#0000ff">new</font> Integer(2)},
{ <font color="#0000ff">new</font> Integer(3), <font color="#0000ff">new</font> Integer(4)},
{ <font color="#0000ff">new</font> Integer(5), <font color="#0000ff">new</font> Integer(6)},
}; </PRE></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
fifth example shows how an array of non-primitive objects can be built up piece
by piece:
</FONT><P></DIV>
<font color="#990000"><PRE> Integer[][] a5;
a5 = <font color="#0000ff">new</font> Integer[3][];
<font color="#0000ff">for</font>(<font color="#0000ff">int</font> i = 0; i < a5.length; i++) {
a5[i] = <font color="#0000ff">new</font> Integer[3];
<font color="#0000ff">for</font>(<font color="#0000ff">int</font> j = 0; j < a5[i].length; j++)
a5[i][j] = <font color="#0000ff">new</font> Integer(i*j);
} </PRE></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>i*j</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is just to put an interesting value into the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Integer</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.</FONT><a name="_Toc375545288"></a><a name="_Toc408018491"></a><P></DIV>
<HR><DIV ALIGN=LEFT><A NAME="fn22" HREF="#fnB22">[22]</A><FONT FACE="Carmina Md BT" SIZE=2 COLOR="Black">
See
</FONT><FONT FACE="Carmina Md BT" SIZE=2 COLOR="Black"><I>Thinking
in C++
</I></FONT><FONT FACE="Carmina Md BT" SIZE=2 COLOR="Black">
for a complete description of aggregate initialization.
</FONT><P></DIV>
<div align="right">
<a href="tij_c.html">Contents</a> | <a href="tij0052.html">Prev</a> | <a href="tij0054.html">Next</a>
</div>
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -