📄 093-097.html
字号:
<HTML>
<HEAD>
<META name=vsisbn content="1558515682"><META name=vstitle content="Java Digital Signal Processing"><META name=vsauthor content="Douglas A. Lyon"><META name=vsimprint content="M&T Books"><META name=vspublisher content="IDG Books Worldwide, Inc."><META name=vspubdate content="11/01/97"><META name=vscategory content="Web and Software Development: Programming, Scripting, and Markup Languages: Java"><TITLE>Java Digital Signal Processing:Java Programming: The Basics</TITLE>
<!-- HEADER --><STYLE type="text/css"> <!-- A:hover { color : Red; } --></STYLE><META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">
<!--ISBN=1558515682//-->
<!--TITLE=Java Digital Signal Processing//-->
<!--AUTHOR=Douglas A. Lyon//-->
<!--PUBLISHER=IDG Books Worldwide, Inc.//-->
<!--IMPRINT=M & T Books//-->
<!--CHAPTER=2//-->
<!--PAGES=093-097//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="088-093.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="097-103.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H4 ALIGN="LEFT"><A NAME="Heading14"></A><FONT COLOR="#000077">Arrays</FONT></H4>
<P>An instance of an array is a reference-type instance. The <I>length</I> variable can be accessed, but not set, and it indicates the number of elements in the array. Array elements start at element zero and end at element length –1.</P>
<P>Any access beyond the end of the length of an array results in the <I>ArrayIndexOutOfBoundsException</I> being thrown. The method <I>arraycopy</I> will copy values between arrays of the same type.</P>
<P>An array can hold any data type, but every element in the array must hold the same data type. Arrays have a fixed length and can be dynamically allocated. As long as there is enough memory, it is possible to make deep arrays. For example:</P>
<!-- CODE //-->
<PRE>
int deep_array[] [] [] [] [] [] [] [] [] [] = new int [2]
[2] [2] [2] [2] [2] [2] [2] [2] [2] ;
int sum = 0;
for (int i=0; i<2; i++) {
deep_array[i] [i] [i] [i] [i] [i] [i] [i] [i] [i] = 1;
sum += deep_array[i] [i] [i] [i] [i] [i] [i] [i]
[i] [i] ;
System.out.println(“sum =” + sum);
}
sum =1
sum =2
</PRE>
<!-- END CODE //-->
<P>As pointed out in Chapter 1, there are both Java-style arrays and C/C++-style arrays.
</P>
<H4 ALIGN="LEFT"><A NAME="Heading15"></A><FONT COLOR="#000077">Vectors</FONT></H4>
<P>A Vector is a container class that can be expanded dynamically. A container class is designed to store reference data types. Vectors are an excellent choice for implementing data structures of variable length. The <I>Vector</I> class resides in the <I>java.util</I> package. The <I>java.util</I> package must be imported before using. Vectors can hold any combination of objects. For example:</P>
<!-- CODE SNIP //-->
<PRE>
Vector v = new Vector();
Rectangle a = new Rectangle(10,20);
v.addElement(A);
</PRE>
<!-- END CODE SNIP //-->
<P>Vectors cannot hold <I>int</I>, <I>float</I>, <I>char</I>, <I>byte</I>, or any of the nonreference data types. The following example shows a series of different data types being added to a <I>Vector</I> instance:</P>
<!-- CODE //-->
<PRE>
static public void add_elements(Vector v) {
for (int i=0; i < number_of_rays.getValue();
i++) {
v.addElement(grating_target_line[i]);
v.addElement(camera_grating_line[i]);
}
v.addElement(camera);
v.addElement(grating);
v.addElement(wedge);
v.addElement(laser);
v.addElement(laser_wedge_line);
} // add_elements
</PRE>
<!-- END CODE //-->
<P>Having a collection of different instances in a <I>Vector</I> instance is a good way to implement polymorphism. With polymorphism, you can cast all the instances stored in the <I>Vector</I> instance into a common superclass that supports a common method. In the following example, <I>drawShapes</I> is a vector that contains several different instances, all of them subclasses of the <I>Shape</I> class. Thus, all of them can be cast into the <I>Shape</I> type. Also, the <I>Shape</I> class has an abstract <I>draw</I> method. Thus, the <I>draw</I> method can be invoked on any subclass of the <I>Shape</I> class provided that it has been properly cast. For example:</P>
<!-- CODE SNIP //-->
<PRE>
for (int i = 0; i < drawnShapes.size(); i++) {
s = (Shape)drawnShapes.elementAt(i);
s.draw(g);
}
</PRE>
<!-- END CODE SNIP //-->
<H4 ALIGN="LEFT"><A NAME="Heading16"></A><FONT COLOR="#000077">Exceptions</FONT></H4>
<P>The grammar of a computer language allows the formulation of code that will compile. However, after compile time comes run time. The Java language specification covers both the syntactic (compile-time) and the semantic (run-time) behavior of the Java environment. Thus, the specification goes beyond the compiler and describes the behavior of the Java virtual machine. The Java virtual machine will throw an exception under special run-time conditions. This throw is a nonlocal transfer of the flow of control from the area that generated the exception to a place that <I>catches</I> the exception.</P>
<P>The MBNF of the <I>try</I> statement follows:</P>
<!-- CODE SNIP //-->
<PRE>
tryStatement ->
“try” statement < “catch” “(“
parameter “)” statement >
[ “finally” statement] .
</PRE>
<!-- END CODE SNIP //-->
<P>Typically, a statement that can possibly fail is surrounded with the <I>try</I> and <I>catch</I> keywords. Failure in the statement following the <I>try</I> can then be intercepted and handled. For example:</P>
<!-- CODE //-->
<PRE>
try {
// create an instance of your applet class
a = (Applet) Class.forName(className).newInstance();
} catch (ClassNotFoundException e) {
System.out.println(“ClassNotFoundException in
AppletFrame”);
return;
} catch (InstantiationException e) {
System.out.println(“InstantiationException in
AppletFrame”);
return;
} catch (IllegalAccessException e) {
System.out.println(“IllegalAccessException in
AppletFrame”);
return;
}
</PRE>
<!-- END CODE //-->
<P>Sometimes no error is to be generated at all; we simply want the program to continue running. For example:
</P>
<!-- CODE SNIP //-->
<PRE>
try {
out.write(buffer);
} catch(Exception e) { }
</PRE>
<!-- END CODE SNIP //-->
<P>You can invent exception names at will by extending the <I>exception</I> class. For example:</P>
<!-- CODE SNIP //-->
<PRE>
class FileFormatException extends Exception {
public FileFormatException(String s) {
super(s);
}
</PRE>
<!-- END CODE SNIP //-->
<P>An overview of the <I>java.lang</I> exception classes is shown in Figure 2.3.</P>
<P><A NAME="Fig3"></A><A HREF="javascript:displayWindow('images/02-03.jpg',467,195 )"><IMG SRC="images/02-03t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/02-03.jpg',467,195)"><FONT COLOR="#000077"><B>Figure 2.3</B></FONT></A> An overview of the <I>java.lang</I> exception classes.
</P>
<P>The <I>java.lang.RuntimeException</I> class hierarchy is shown in Figure 2.4.</P>
<P><A NAME="Fig4"></A><A HREF="javascript:displayWindow('images/02-04.jpg',528,210 )"><IMG SRC="images/02-04t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/02-04.jpg',528,210)"><FONT COLOR="#000077"><B>Figure 2.4</B></FONT></A> An overview of the <I>java.lang.Runtime Exception</I> classes.
<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="088-093.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="097-103.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<hr width="90%" size="1" noshade><div align="center"><font face="Verdana,sans-serif" size="1">Copyright © <a href="/reference/idgbooks00001.html">IDG Books Worldwide, Inc.</a></font></div>
<!-- all of the reference materials (books) have the footer and subfoot reveresed --><!-- reference_subfoot = footer --><!-- reference_footer = subfoot --></BODY></HTML><!-- END FOOTER -->
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -