⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 10.doc.html

📁 java语言规范
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<a name="25758"></a>
As an example:<p>
<pre><a name="25759"></a>
class Test {
<a name="25765"></a>	public static void main(String[] args) {
<a name="25760"></a>		int ia[][] = { {1, 2}, null };
<a name="25761"></a>		for (int i = 0; i &lt; 2; i++)
<a name="25762"></a>			for (int j = 0; j &lt; 2; j++)
<a name="25763"></a>				System.out.println(ia[i][j]);
<a name="25766"></a>	}
<a name="25764"></a>}
</pre><a name="25767"></a>
prints:
<p><pre><a name="25769"></a>
1
<a name="25770"></a>2
</pre><a name="25771"></a>
before causing a <code>NullPointerException</code> in trying to index the second component
of the array <code>ia</code>, which is a null reference.
<p><a name="11364"></a>
<h2>10.7    Array Members</h2>
<a name="25782"></a>
The members of an array type are all of the following:
<p><ul><a name="25783"></a>
<li>The <code>public</code> <code>final</code> field <code>length</code>, which contains the number of components of the array (<code>length</code> may be positive or zero)
<a name="42464"></a>
<li>The <code>public</code> method <code>clone</code>, which overrides the method of the same name in class <code>Object</code> and throws no checked exceptions
<a name="25784"></a>
<li>All the members inherited from class <code>Object</code>; the only method of <code>Object</code> that is not inherited is its <code>clone</code> method
</ul><a name="29781"></a>
An array thus has the same methods as the following class:
<p><pre><a name="29785"></a>
class A implements Cloneable {
<a name="29786"></a>	public final int length = <i>X</i>;
<a name="29787"></a>	public Object clone() {
<a name="50155"></a>		try {
<a name="29797"></a>			return super.clone();
<a name="50157"></a>		} catch (CloneNotSupportedException e) {
<a name="29798"></a>			throw new InternalError(e.getMessage());
<a name="29793"></a>		}
<a name="29794"></a>	}
<br><a name="29790"></a>}
</pre><a name="25789"></a>
Every array implements interface <code>Cloneable</code>. That arrays are cloneable is shown 
by the test program:
<p><pre><a name="25795"></a>
class Test {
<a name="25796"></a>	public static void main(String[] args) {
<a name="25801"></a>		int ia1[] = { 1, 2 };
<a name="25802"></a>		int ia2[] = (int[])ia1.clone();
<a name="25803"></a>		System.out.print((ia1 == ia2) + " ");
<a name="25810"></a>		ia1[1]++;
<a name="25811"></a>		System.out.println(ia2[1]);
<a name="25804"></a>	}
<a name="25805"></a>}
</pre><a name="25812"></a>
which prints:
<p><pre><a name="25813"></a>false 2
</pre><a name="25815"></a>
showing that the components of the arrays referenced by <code>ia1</code> and <code>ia2</code> are different 
variables. (In some early implementations of Java this example failed to compile 
because the compiler incorrectly believed that the clone method for an array could 
throw a <code>CloneNotSupportedException</code>.)
<p><a name="25833"></a>
A <code>clone</code> of a multidimensional array is shallow, which is to say that it creates only a single new array. Subarrays are shared, as shown by the example program:<p>
<pre><a name="25843"></a>
class Test {
<a name="25844"></a>	public static void main(String[] args) throws Throwable {
<a name="25845"></a>		int ia[][] = { { 1 , 2}, null };
<a name="25846"></a>		int ja[][] = (int[][])ia.clone();
<a name="25858"></a>		System.out.print((ia == ja) + " ");
<a name="25847"></a>		System.out.println(ia[0] == ja[0] &amp;&amp; ia[1] == ja[1]);
<a name="25849"></a>	}
<a name="25850"></a>}
</pre><a name="25855"></a>
which prints:
<p><pre><a name="25856"></a>false true
</pre><a name="28553"></a>
showing that the <code>int[]</code> array that is <code>ia[0]</code> and the <code>int[]</code> array that is <code>ja[0]</code> are 
the same array.
<p><a name="40879"></a>
<h2>10.8    <code>Class</code> Objects for Arrays</h2>
<a name="40882"></a>
Every array has an associated <code>Class</code> object, shared with all other arrays with the 
same component type. The superclass of an array type is considered to be <code>Object</code>, 
as shown by the following example code:
<p><pre><a name="40886"></a>
class Test {
<a name="40887"></a>	public static void main(String[] args) {
<a name="40888"></a>		int[] ia = new int[3];
<a name="40889"></a>		System.out.println(ia.getClass());
<a name="40890"></a>		System.out.println(ia.getClass().getSuperclass());
<a name="40891"></a>	}
<a name="40892"></a>}
</pre><a name="40893"></a>
which prints:
<p><pre><a name="40894"></a>
class [I
<a name="40895"></a>class java.lang.Object
</pre><a name="40896"></a>
where the string "<code>[I</code>" is the run-time type signature for the class object "array 
with component type <code>int</code>" <a href="javalang.doc1.html#13783">(&#167;20.1.1)</a>.
<p><a name="25726"></a>
<h2>10.9    An Array of Characters is Not a <code>String</code></h2>
<a name="25730"></a>
In Java, unlike C, an array of <code>char</code> is not a <code>String</code> <a href="javalang.doc11.html#14460">(&#167;20.12)</a>, and neither a <code>String</code> 
nor an array of <code>char</code> is terminated by <code>'\u0000'</code> (the NUL character).
<p><a name="25731"></a>
A Java <code>String</code> object is immutable, that is, its contents never change, while an array of <code>char</code> has mutable elements. The method <code>toCharArray</code> in class <code>String</code> returns an array of characters containing the same character sequence as a <code>String</code>. The class <code>StringBuffer</code> implements useful methods on mutable arrays of characters <a href="javalang.doc12.html#14461">(&#167;20.13)</a>.<p>
<a name="11430"></a>
<h2>10.10    Array Store Exception</h2>
<a name="26003"></a>
If an array variable <i>v</i><i></i> has type <i>A</i><i></i><code>[]</code>, where <i>A</i><i></i> is a reference type, then <i>v</i><i></i> can hold a 
reference to an instance of any array type <i>B</i><code>[]</code>, provided <i>B</i><i></i> can be assigned to <i>A</i><i></i>.
<p><a name="53134"></a>
Thus, the example:<p>
<pre><a name="26004"></a>
class Point { int x, y; }
<br><a name="53558"></a>class ColoredPoint extends Point { int color; }
<br></pre><pre><a name="53993"></a>
class Test {
<a name="26007"></a>	public static void main(String[] args) {
<a name="26008"></a>		ColoredPoint[] cpa = new ColoredPoint[10];
<a name="26009"></a>		Point[] pa = cpa;
<a name="26010"></a>		System.out.println(pa[1] == null);
<a name="26011"></a>		try {
<a name="26012"></a>			pa[0] = new Point();
<a name="26013"></a>		} catch (ArrayStoreException e) {
<a name="26014"></a>			System.out.println(e);
<a name="26015"></a>		}
<a name="26016"></a>	}
<a name="26017"></a>}
</pre><a name="25743"></a>
produces the output:
<p><pre><a name="25746"></a>
true
<a name="25744"></a>java.lang.ArrayStoreException
</pre><a name="26018"></a>
Here the variable <code>pa</code> has type <code>Point[]</code> and the variable <code>cpa</code> has as its value a reference
to an object of type <code>ColoredPoint[]</code>. A <code>ColoredPoint</code> can be assigned 
to a <code>Point</code>; therefore, the value of <code>cpa</code> can be assigned to <code>pa</code>.
<p><a name="26025"></a>
A reference to this array <code>pa</code>, for example, testing whether <code>pa[1]</code> is <code>null</code>, will not result in a run-time type error. This is because the element of the array of type <code>ColoredPoint[]</code> is a <code>ColoredPoint</code>, and every <code>ColoredPoint</code> can stand in for a <code>Point</code>, since <code>Point</code> is the superclass of <code>ColoredPoint</code>.<p>
<a name="26035"></a>
On the other hand, an assignment to the array <code>pa</code> can result in a run-time error. At compile time, an assignment to an element of <code>pa</code> is checked to make sure that the value assigned is a <code>Point</code>. But since <code>pa</code> holds a reference to an array of <code>ColoredPoint</code>, &#32;the assignment is valid only if the type of the value assigned at run-time is, more specifically, a <code>ColoredPoint</code>.<p>
<a name="26044"></a>
Java checks for such a situation at run-time to ensure that the assignment is valid; if not, an <code>ArrayStoreException</code> is thrown. More formally: an assignment to an element of an array whose type is <i>A</i><code>[]</code>, where <i>A</i> is a reference type, is checked at run-time to ensure that the value assigned can be assigned to the actual element type of the array, where the actual element type may be any reference type that is assignable to <i>A</i>.<p>


<hr>
<!-- This inserts footnotes--><p>
<a href="index.html">Contents</a> | <a href="9.doc.html">Prev</a> | <a href="11.doc.html">Next</a> | <a href="j.index.doc1.html">Index</a>
<p>
<font size=-1>Java Language Specification (HTML generated by Suzette Pelouch on February 24, 1998)<br>
<i><a href="jcopyright.doc.html">Copyright &#169 1996 Sun Microsystems, Inc.</a>
All rights reserved</i>
<br>
Please send any comments or corrections to <a href="mailto:doug.kramer@sun.com">doug.kramer@sun.com</a>
</font>
</body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -