📄 chap15.html
字号:
<P>18 iconst_0 // Push constant int 0.</P>
<P>19 istore_3 // Pop int into local variable 3: int k = 0;</P>
<P>20 goto 38 // Go to section of code that tests inner loop.</P>
<P>23 aload_0 // Push object ref from local variable 0.</P>
<P>24 iload_1 // Push int from local variable 1 (i).</P>
<P>25 aaload // Pop index and arrayref, push object ref</P>
<P> // at arrayref[index] (gets threeD[i]).</P>
<P>26 iload_2 // Push int from local variable 2 (j).</P>
<P>27 aaload // Pop index and arrayref, push object ref</P>
<P> // at arrayref[index] (gets threeD[i][j]).</P>
<P>28 iload_3 // Push int from local variable 3 (k).</P>
<P> // Now calculate the int that will be assigned</P>
<P> // to threeD[i][j][k]</P>
<P>29 iload_1 // Push int from local variable 1 (i).</P>
<P>30 iload_2 // Push int from local variable 2 (j).</P>
<P>31 iadd // Pop two ints, add them, push int result (i + j).</P>
<P>32 iload_3 // Push int from local variable 3 (k).</P>
<P>33 iadd // Pop two ints, add them, push int</P>
<P> // result (i + j + k).</P>
<P>34 iastore // Pop value, index, and arrayref; assign</P>
<P> // arrayref[index] = value:</P>
<P> // threeD[i][j][k] = i + j + k;</P>
<P>35 iinc 3 1 // Increment by 1 the int in local variable 3: ++k;</P>
<P>38 iload_3 // Push int from local variable 3 (k).</P>
<P>39 iconst_3 // Push constant int 3.</P>
<P>40 if_icmplt 23 // Pop right and left ints, jump if left < right:</P>
<P> // for (...; k < 3;...)</P>
<P>43 iinc 2 1 // Increment by 1 the int in local variable 2: ++j;</P>
<P>46 iload_2 // Push int from local variable 2 (j).</P>
<P>47 iconst_4 // Push constant int 4.</P>
<P>48 if_icmplt 18 // Pop right and left ints, jump if left < right:</P>
<P> // for (...; j < 4;...)</P>
<P>51 iinc 1 1 // Increment by 1 the int in local variable 1: ++i;</P>
<P>54 iload_1 // Push int from local variable 1 (i).</P>
<P>55 iconst_5 // Push constant int 5.</P>
<P>56 if_icmplt 13 // Pop right and left ints, jump if left < right:</P>
<P> // for (...; i < 5;...)</P>
<P>59 return</P>
</FONT><FONT SIZE="2"><P> </P></FONT><FONT FACE="Courier New">end</FONT></P></PRE>
<P>The <CODE>initAnArray()</CODE> method merely allocates and initializes a three-dimensional array. This simulation demonstrates how the Java Virtual Machine handles multidimensional arrays. In response to the <FONT FACE="Courier New">multianewarray</FONT> instruction, which in this example requests the allocation of a three-dimensional array, the Java Virtual Machine creates a tree of one-dimensional arrays. The reference returned by the <FONT FACE="Courier New">multianewarray</FONT> instruction refers to the base one-dimensional array in the tree. In the <CODE>initAnArray()</CODE> method, the base array has five components--<CODE>threeD[0]</CODE> through <CODE>threeD[4]</CODE>. Each component of the base array is itself a reference to a one-dimensional array of four components, accessed by <CODE>threeD[0][0]</CODE> through <CODE>threeD[4][3]</CODE>. The components of these five arrays are also references to arrays, each of which has three components. These components are <FONT FACE="Courier New">int</FONT>s, the elements of this multidimensional array, and they are accessed by <CODE>threeD[0][0][0]</CODE> through <CODE>threeD[4][3][2]</CODE>. </P>
<P>In response to the <FONT FACE="Courier New">multianewarray</FONT> instruction in the <CODE>initAnArray()</CODE> method, the Java Virtual Machine creates one five-dimensional array of arrays, five four-dimensional arrays of arrays, and twenty three-dimensional arrays of <CODE>ints</CODE>. The Java Virtual Machine allocates these 26 arrays on the heap, initializes their components such that they form a tree, and returns the reference to the base array. </P>
<P>To assign an <CODE>int</CODE> value to an element of the three-dimensional array, the Java Virtual Machine uses <FONT FACE="Courier New">aaload</FONT> to get a component of the base array. Then the Java Virtual Machine uses <FONT FACE="Courier New">aaload</FONT> again on this component--which is itself an array of arrays--to get a component of the branch array. This component is a reference to a leaf array of <CODE>ints</CODE>. Finally the Java Virtual Machine uses <FONT FACE="Courier New">iastore</FONT> to assign an <CODE>int</CODE> value to the element of the leaf array. The Java Virtual Machine uses multiple one-dimensional array accesses to accomplish operations on multidimensional arrays. </P>
<P>To drive the <I>Three-Dimensional Array</I> simulation, use the Step, Reset, Run, and Stop buttons. Each time you press the Step button, the simulator will execute the instruction pointed to by the pc register. If you press the Run button, the simulation will continue with no further coaxing on your part until you press the Stop button. To start the simulation over, press the Reset button. For each step of the simulation, a panel at the bottom of the applet contains an explanation of what the next instruction will do. Happy clicking.</P>
<P><IMG SRC="fig15-1.gif" tppabs="http://www.pbg.mcgraw-hill.com/betabooks/venners/images/fig15-1.gif" ALT="Figure 15-1"></P>
<H3><EM><P>On the CD-ROM</P>
</EM></H3><P> The CD-ROM contains the source code examples from this chapter in the <FONT FACE="Courier New">opcodes</FONT> directory. The <I>Three-Dimensional Array</I> applet is part of a web page on the CD-ROM in file <FONT FACE="Courier New">applets/ThreeDArray.html</FONT>. The source code for this applet is found alongside its class files, in the <FONT FACE="Courier New">applets/JVMSimulators</FONT> and <FONT FACE="Courier New">applets/JVMSimulators/COM/artima/jvmsim</FONT> directories.</P>
<H3><EM><P>The Resources Page</P>
</EM></H3><P>For more information about the material presented in this chapter, visit the resources page: <FONT FACE="Courier New"><A HREF="http://www.artima.com/insidejvm/objects.html" tppabs="http://www.artima.com/insidejvm/objects.html">http://www.artima.com/insidejvm/objects.html</A></FONT>.</P>
<TABLE BORDER="0" WIDTH="100%">
<TR><TD><A HREF="http://www.pbg.mcgraw-hill.com/betabooks/stores.html" tppabs="http://www.pbg.mcgraw-hill.com/betabooks/stores.html" target="bottom"><IMG SRC="hotkey.gif" tppabs="http://www.pbg.mcgraw-hill.com/betabooks/images/hotkey.gif" ALIGN="LEFT" BORDER="0" WIDTH="40" HEIGHT="40" ALT="Orders"></A>
<IMG SRC="order_text.gif" tppabs="http://www.pbg.mcgraw-hill.com/betabooks/images/order_text.gif" WIDTH="103" HEIGHT="41" ALT="Orders"></TD>
<TD ALIGN="RIGHT"><A HREF="chap14.html" tppabs="http://www.pbg.mcgraw-hill.com/betabooks/venners/chap14.html"><IMG SRC="backward.gif" tppabs="http://www.pbg.mcgraw-hill.com/betabooks/images/backward.gif" BORDER="0" ALT="Backward" WIDTH="32" HEIGHT="32"></A> <A HREF="chap16.html" tppabs="http://www.pbg.mcgraw-hill.com/betabooks/venners/chap16.html"><IMG SRC="forward.gif" tppabs="http://www.pbg.mcgraw-hill.com/betabooks/images/forward.gif" BORDER="0" ALT="Forward" WIDTH="32" HEIGHT="32"></A></TD></TR>
<TR><TD COLSPAN="2"><A HREF="mailto:computing@mcgraw-hill.com"><IMG SRC="hotkey.gif" tppabs="http://www.pbg.mcgraw-hill.com/betabooks/images/hotkey.gif" ALIGN="LEFT" BORDER="0" WIDTH="40" HEIGHT="40" ALT="Comments"></A>
<IMG SRC="comment_text.gif" tppabs="http://www.pbg.mcgraw-hill.com/betabooks/images/comment_text.gif" WIDTH="73" HEIGHT="39" ALT="Comments"></TD></TR>
</TABLE>
<HR>
<P ALIGN=CENTER> <A HREF="http://www.pbg.mcgraw-hill.com/computing/computing-home.html" tppabs="http://www.pbg.mcgraw-hill.com/computing/computing-home.html" TARGET="_top">COMPUTING
MCGRAW-HILL</A> | <A HREF="http://www.pbg.mcgraw-hill.com/betabooks/betabooks-home.html" tppabs="http://www.pbg.mcgraw-hill.com/betabooks/betabooks-home.html" TARGET="_top">Beta Books</A>
| <A HREF="http://www.pbg.mcgraw-hill.com/computing/contact.html" tppabs="http://www.pbg.mcgraw-hill.com/computing/contact.html" TARGET="_top">Contact Us</A>
| <A HREF="http://www.pbg.mcgraw-hill.com/betabooks/stores.html" tppabs="http://www.pbg.mcgraw-hill.com/betabooks/stores.html" TARGET="_top">Order Information</A>
| <A HREF="http://mcgraw-hill.inforonics.com/compsearch.shtml" tppabs="http://mcgraw-hill.inforonics.com/compsearch.shtml" TARGET="_top">Online Catalog</A></P>
<P ALIGN=CENTER><FONT SIZE="-1"><A HREF="http://www.pbg.mcgraw-hill.com/computing/computing-home.html" tppabs="http://www.pbg.mcgraw-hill.com/computing/computing-home.html" TARGET="_top">Computing McGraw-Hill</A> is an imprint of the <A HREF="http://www.pbg.mcgraw-hill.com/pbg-home.html" tppabs="http://www.pbg.mcgraw-hill.com/pbg-home.html" TARGET="_top">McGraw-Hill Professional Book Group</A>.</FONT></P>
<!-- begin footer -->
<HR>
<A HREF="http://www.mcgraw-hill.com/" tppabs="http://www.mcgraw-hill.com/" TARGET="_top"><IMG SRC="division-white.gif" tppabs="http://www.pbg.mcgraw-hill.com/images/division-white.gif" WIDTH="350" HEIGHT="44" ALT="A Division of the McGraw-Hill Companies" BORDER="0"></A><BR>
<FONT SIZE="-2">Copyright © 1997 <A HREF="http://www.mcgraw-hill.com/" tppabs="http://www.mcgraw-hill.com/" TARGET="_top">
The McGraw-Hill Companies</A>. All rights reserved. Any use is subject to the
<A HREF="http://www.mcgraw-hill.com/corporate/news_info/copyrttm.htm" tppabs="http://www.mcgraw-hill.com/corporate/news_info/copyrttm.htm" TARGET="_top">
Terms of Use</A>; the corporation also has a comprehensive <A HREF="http://www.mcgraw-hill.com/corporate/news_info/privacy.html" tppabs="http://www.mcgraw-hill.com/corporate/news_info/privacy.html" TARGET="_top">
Privacy Policy</A> governing information we may collect from our customers.</FONT>
<!-- end footer -->
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -