📄 ch36.htm
字号:
</TD></TR><TR VALIGN=TOP><TD WIDTH=166><TT>log()</TT></TD><TD WIDTH=424>Returns the natural log of a value.</TD></TR><TR VALIGN=TOP><TD WIDTH=166><TT>max()</TT></TD><TD WIDTH=424>Returns the greater of two values.</TD></TR><TR VALIGN=TOP><TD WIDTH=166><TT>min()</TT></TD><TD WIDTH=424>Returns the smaller of two values.</TD></TR><TR VALIGN=TOP><TD WIDTH=166><TT>random()</TT></TD><TD WIDTH=424>Returns a random number between 0.0 and 1.0.</TD></TR><TR VALIGN=TOP><TD WIDTH=166><TT>round()</TT></TD><TD WIDTH=424>Rounds a floating-point or double number.</TD></TR><TR VALIGN=TOP><TD WIDTH=166><TT>sin()</TT></TD><TD WIDTH=424>Returns the sine of an angle.</TD></TR><TR VALIGN=TOP><TD WIDTH=166><TT>sqrt()</TT></TD><TD WIDTH=424>Returns the square root of a value.</TD></TR><TR VALIGN=TOP><TD WIDTH=166><TT>tan()</TT></TD><TD WIDTH=424>Returns the tangent of an angle.</TD></TR></TABLE></CENTER><P><P>To call any of the math methods, reference them through the <TT>Math</TT>class, like this:<BLOCKQUOTE><PRE>Math.Method()</PRE></BLOCKQUOTE><P>For example, to get the square root of 10, you use this line:<BLOCKQUOTE><PRE>double result = Math.sqrt(10);</PRE></BLOCKQUOTE><H3><A NAME="TheIStringIClass">The <I>String </I>Class</A></H3><P>You're no stranger to the <TT>String</TT> class. You've used itin a number of programs in order to store and manipulate textstrings. However, because this class is so useful to a programmer,you'll take a closer look at it here. As you'll see, the <TT>String</TT>class is powerful, enabling you to manipulate strings in moreways than you may have realized. Table 36.5 shows the most commonlyused methods of the <TT>String</TT> class.<BR><P><CENTER><B>Table 36.5 Methods of the </B><I>String</I><B>Class.</B></CENTER><P><CENTER><TABLE BORDER=1 WIDTH=80%><TR VALIGN=TOP><TD WIDTH=195><I><B>Method</B></I></TD><TD WIDTH=395><I><B>Description</B></I></TD></TR><TR VALIGN=TOP><TD WIDTH=195><TT>charAt()</TT></TD><TD WIDTH=395>Returns the character at the given string index.</TD></TR><TR VALIGN=TOP><TD WIDTH=195><TT>compareTo()</TT></TD><TD WIDTH=395>Compares a string to another string.</TD></TR><TR VALIGN=TOP><TD WIDTH=195><TT>concat()</TT></TD><TD WIDTH=395>Joins two strings.</TD></TR><TR VALIGN=TOP><TD WIDTH=195><TT>copyValueOf()</TT></TD><TD WIDTH=395>Copies a character array to a string.</TD></TR><TR VALIGN=TOP><TD WIDTH=195><TT>endsWith()</TT></TD><TD WIDTH=395>Checks a string for the given suffix.</TD></TR><TR VALIGN=TOP><TD WIDTH=195><TT>equals()</TT></TD><TD WIDTH=395>Compares a string to another object.</TD></TR><TR VALIGN=TOP><TD WIDTH=195><TT>equalsIgnoreCase()</TT></TD><TD WIDTH=395>Compares a string to another object with no regard for upper- or lowercase.</TD></TR><TR VALIGN=TOP><TD WIDTH=195><TT>getBytes()</TT></TD><TD WIDTH=395>Copies selected characters from a string to a byte array.</TD></TR><TR VALIGN=TOP><TD WIDTH=195><TT>getChars()</TT></TD><TD WIDTH=395>Copies selected characters from a string to a character array.</TD></TR><TR VALIGN=TOP><TD WIDTH=195><TT>hashCode()</TT></TD><TD WIDTH=395>Returns a string's hashcode.</TD></TR><TR VALIGN=TOP><TD WIDTH=195><TT>indexOf()</TT></TD><TD WIDTH=395>Finds the index of the first occurrence of a given character or substring in a string.</TD></TR><TR VALIGN=TOP><TD WIDTH=195><TT>lastIndexOf()</TT></TD><TD WIDTH=395>Finds the index of the last occurrence of a given character or substring in a string.</TD></TR><TR VALIGN=TOP><TD WIDTH=195><TT>length()</TT></TD><TD WIDTH=395>Returns the length of a string.</TD></TR><TR VALIGN=TOP><TD WIDTH=195><TT>regionMatches()</TT></TD><TD WIDTH=395>Compares a portion of a string to a portion of another string.</TD></TR><TR VALIGN=TOP><TD WIDTH=195><TT>replace()</TT></TD><TD WIDTH=395>Replaces all occurrences of a given character with a new character.</TD></TR><TR VALIGN=TOP><TD WIDTH=195><TT>startsWith()</TT></TD><TD WIDTH=395>Checks a string for the given prefix.</TD></TR><TR VALIGN=TOP><TD WIDTH=195><TT>substring()</TT></TD><TD WIDTH=395>Returns a substring of a string.</TD></TR><TR VALIGN=TOP><TD WIDTH=195><TT>toCharArray()</TT></TD><TD WIDTH=395>Converts a string to a character array.</TD></TR><TR VALIGN=TOP><TD WIDTH=195><TT>toLowerCase()</TT></TD><TD WIDTH=395>Converts all characters in the string to lowercase.</TD></TR><TR VALIGN=TOP><TD WIDTH=195><TT>toUpperCase()</TT></TD><TD WIDTH=395>Converts all characters in the string to uppercase.</TD></TR><TR VALIGN=TOP><TD WIDTH=195><TT>trim()</TT></TD><TD WIDTH=395>Removes whitespace characters from the beginning and end of a string.</TD></TR><TR VALIGN=TOP><TD WIDTH=195><TT>valueOf()</TT></TD><TD WIDTH=395>Returns a string representation of an object.</TD></TR></TABLE></CENTER><P><H3><A NAME="ExampleUsingtheIStringIClass">Example: Using the <I>String</I> Class</A></H3><P>Listing 36.3 is an applet that shows you how a few of the <TT>String</TT>methods you haven't tried yet work. When you run the applet withAppletviewer, you see the window shown in Figure 36.3. The applettakes whatever text strings you type in the two text boxes, andcompares them for equality without considering upper- or lowercase.It then concatenates the strings and displays the new concatenatedstring along with its length.<P><A HREF="f36-3.gif"><B> Figure 36.3 : </B><I>This is StringApplet running under Appletviewer.</I></A><P><HR><BLOCKQUOTE><B>Listing 36.3 StringApplet.java: An Applet That ManipulatesStrings.<BR></B></BLOCKQUOTE><BLOCKQUOTE><PRE>import java.awt.*;import java.applet.*;public class StringApplet extends Applet{ TextField textField1; TextField textField2; public void init() { textField1 = new TextField(20); textField2 = new TextField(20); textField1.setText("STRING"); textField2.setText("String"); add(textField1); add(textField2); } public void paint(Graphics g) { String str1 = textField1.getText(); String str2 = textField2.getText(); boolean equal = str1.equalsIgnoreCase(str2); if (equal) g.drawString("The strings are equal.", 70, 100); else g.drawString("The strings are not equal.", 70, 100); String newStr = str1.concat(str2); g.drawString("JOINED STRINGS:", 70, 130); g.drawString(newStr, 80, 150); g.drawString("STRING LENGTH:", 70, 180); int length = newStr.length(); String s = String.valueOf(length); g.drawString(s, 80, 200); } public boolean action(Event evt, Object arg) { repaint(); return true; }}</PRE></BLOCKQUOTE><HR><P><IMG ALIGN=RIGHT SRC="pseudo.gif" HEIGHT=94 WIDTH=94 BORDER=1><BLOCKQUOTE>Tell Java that the application uses the <TT>awt</TT> package.<BR>Tell Java that the application uses the <TT>applet</TT> package.<BR>Derive the <TT>StringApp</TT> class from <TT>Applet</TT>.<BR> Declare the class's data fields.<BR> Override the <TT>init()</TT> method.<BR> Create two <TT>TextField</TT> controls.<BR> Set the controls' contents.<BR> Add the controls to the applet's layout.<BR> Override the <TT>paint()</TT> method.<BR> Get the contents of the two text boxes.<BR> Compare the two strings.<BR> Display the appropriate message about the strings' equality.<BR> Concatenate the two strings.<BR> Display the joined strings.<BR> Get and display the new string's length.<BR> Override the <TT>action()</TT> method.<BR> Force Java to repaint the applet.<BR> Tell Java that the action was handled okay.</BLOCKQUOTE><H2><A NAME="TheIioIPackage"><FONT SIZE=5 COLOR=#Ff0000>The <I>io</I> Package</FONT></A></H2><P>Although Java applets are extremely limited in their I/O abilities,Java applications are free to create, load, and save files justlike any other application. All of Java's file-handling abilitiesare housed in the <TT>io</TT> package. This package includes manyclasses that enable you to handle files and other types of inputand output streams. Table 33.6 lists the more commonly used ofthese classes and their descriptions:<BR><P><CENTER><B>Table 36.6 Commonly Used Classes in the</B><I>io</I><B> Package.</B></CENTER><P><CENTER><TABLE BORDER=1 WIDTH=80%><TR VALIGN=TOP><TD WIDTH=243><I><B>Class</B></I></TD><TD WIDTH=347><I><B>Description</B></I></TD></TR><TR VALIGN=TOP><TD WIDTH=243><TT>BufferedInputStream</TT></TD><TD WIDTH=347>An input stream that buffers data.</TD></TR><TR VALIGN=TOP><TD WIDTH=243><TT>BufferedOutputStream</TT></TD><TD WIDTH=347>An output stream that buffers data.</TD></TR><TR VALIGN=TOP><TD WIDTH=243><TT>DataInputStream</TT></TD><TD WIDTH=347>An input stream for reading primitive Java data types.</TD></TR><TR VALIGN=TOP><TD WIDTH=243><TT>DataOutputStream</TT></TD><TD WIDTH=347>An output stream for writing primitive Java data types.</TD></TR><TR VALIGN=TOP><TD WIDTH=243><TT>File</TT></TD><TD WIDTH=347>Represents a file.</TD></TR><TR VALIGN=TOP><TD WIDTH=243><TT>FileInputStream</TT></TD><TD WIDTH=347>An input stream associated with a file.</TD></TR><TR VALIGN=TOP><TD WIDTH=243><TT>FileOutputStream</TT></TD><TD WIDTH=347>An output stream associated with a file.</TD></TR><TR VALIGN=TOP><TD WIDTH=243><TT>InputStream</TT></TD><TD WIDTH=347>The superclass from which input classes are derived.</TD></TR><TR VALIGN=TOP><TD WIDTH=243><TT>OutputStream</TT></TD><TD WIDTH=347>The superclass from which output classes are derived.</TD></TR><TR VALIGN=TOP><TD WIDTH=243><TT>PrintStream</TT></TD><TD WIDTH=347>An output stream that can be used for printing.</TD></TR><TR VALIGN=TOP><TD WIDTH=243><TT>PushbackInputStream</TT></TD><TD WIDTH=347>An input stream that enables a program to return read values back into the stream.</TD></TR><TR VALIGN=TOP><TD WIDTH=243><TT>RandomAccessFile</TT></TD><TD WIDTH=347>Represents random-access files.</TD></TR><TR VALIGN=TOP><TD WIDTH=243><TT>StringBufferInputStream</TT></TD><TD WIDTH=347>An input stream whose data source is a string.</TD></TR></TABLE></CENTER><P><H3><A NAME="ExampleReadingaFile">Example: Reading a File</A></H3><P>There are many ways to read files using Java's I/O classes. Themost basic, however, is to read a file byte-by-byte. Suppose,for example, you wanted to display on the screen the source codein the file test.java. Listing 33.4 shows such an application.Although this example is very basic, it demonstrates how to useone of Java's I/O classes, <TT>FileInputStream</TT>. Creatinga file using an output stream isn't much different; you just needto create an output stream object and write, rather than read,data. Figure 36.4 shows FileApp's output.<P><A HREF="f36-4.gif"><B> Figure 36.4 : </B><I>The FileApp application reads and displays a text file.</I></A><P><HR><BLOCKQUOTE>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -