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

📄 tij0111.html

📁 学习java的经典书籍
💻 HTML
📖 第 1 页 / 共 3 页
字号:
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>File</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
object out of it, then call 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>getName(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
which strips away all the path information (in a platform-independent way). Then 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>accept(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
uses the <A NAME="Index1067"></A><A NAME="Index1068"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>String
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">class
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>indexOf(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
method to see if the search string 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>afn</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
appears anywhere in the name of the file. If 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>afn</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is found within the string, the return value is the starting index of 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>afn</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
but if it&#8217;s not found the return value is -1. Keep in mind that this is a
simple string search and does not have regular expression
&#8220;wildcard&#8221; matching such as &#8220;fo?.b?r*&#8221; which is much
more difficult to implement.
</FONT><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>list(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
method returns an array. You can query this array for its length and then move
through it selecting the array elements. This ability to easily pass an array
in and out of a method is a tremendous improvement over the behavior of C and
C++.
</FONT><P></DIV>
<A NAME="Heading313"></A><H4 ALIGN=LEFT>
Anonymous
inner classes
</H4>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">This
example is ideal for rewriting using an <A NAME="Index1069"></A><A NAME="Index1070"></A><A NAME="Index1071"></A>anonymous
inner class (described in Chapter 7). As a first cut, a method 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>filter(&#160;)
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">is
created that returns a handle to a 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>FilenameFilter</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">:</FONT><P></DIV>

<font color="#990000"><PRE><font color="#009900">//: DirList2.java</font>
<font color="#009900">// Uses Java 1.1 anonymous inner classes</font>
<font color="#0000ff">import</font> java.io.*;

<font color="#0000ff">public</font> <font color="#0000ff">class</font> DirList2 {
  <font color="#0000ff">public</font> <font color="#0000ff">static</font> FilenameFilter 
  filter(<font color="#0000ff">final</font> String afn) {
    <font color="#009900">// Creation of anonymous inner class:</font>
    <font color="#0000ff">return</font> <font color="#0000ff">new</font> FilenameFilter() {
      String fn = afn;
      <font color="#0000ff">public</font> <font color="#0000ff">boolean</font> accept(File dir, String n) {
        <font color="#009900">// Strip path information:</font>
        String f = <font color="#0000ff">new</font> File(n).getName();
        <font color="#0000ff">return</font> f.indexOf(fn) != -1;
      }
    }; <font color="#009900">// End of anonymous inner class</font>
  }
  <font color="#0000ff">public</font> <font color="#0000ff">static</font> <font color="#0000ff">void</font> main(String[] args) {
    <font color="#0000ff">try</font> {
      File path = <font color="#0000ff">new</font> File(".");
      String[] list;
      <font color="#0000ff">if</font>(args.length == 0)
        list = path.list();
      <font color="#0000ff">else</font> 
        list = path.list(filter(args[0]));
      <font color="#0000ff">for</font>(<font color="#0000ff">int</font> i = 0; i &lt; list.length; i++)
        System.out.println(list[i]);
    } <font color="#0000ff">catch</font>(Exception e) {
      e.printStackTrace();
    }
  }
} <font color="#009900">///:~ </PRE></font></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Note
that the argument to 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>filter(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
must be <A NAME="Index1072"></A><A NAME="Index1073"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>final</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
This is required by the anonymous inner class so that it can use an object from
outside its scope.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">This
design is an improvement because the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>FilenameFilter</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
class is now tightly bound to 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>DirList2</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
However, you can take this approach one step further and define the anonymous
inner class as an argument to 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>list(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
in which case it&#8217;s even smaller:
</FONT><P></DIV>

<font color="#990000"><PRE><font color="#009900">//: DirList3.java</font>
<font color="#009900">// Building the anonymous inner class "in-place"</font>
<font color="#0000ff">import</font> java.io.*;

<font color="#0000ff">public</font> <font color="#0000ff">class</font> DirList3 {
  <font color="#0000ff">public</font> <font color="#0000ff">static</font> <font color="#0000ff">void</font> main(<font color="#0000ff">final</font> String[] args) {
    <font color="#0000ff">try</font> {
      File path = <font color="#0000ff">new</font> File(".");
      String[] list;
      <font color="#0000ff">if</font>(args.length == 0)
        list = path.list();
      <font color="#0000ff">else</font> 
        list = path.list(
          <font color="#0000ff">new</font> FilenameFilter() {
            <font color="#0000ff">public</font> <font color="#0000ff">boolean</font> 
            accept(File dir, String n) {
              String f = <font color="#0000ff">new</font> File(n).getName();
              <font color="#0000ff">return</font> f.indexOf(args[0]) != -1;
            }
          });
      <font color="#0000ff">for</font>(<font color="#0000ff">int</font> i = 0; i &lt; list.length; i++)
        System.out.println(list[i]);
    } <font color="#0000ff">catch</font>(Exception e) {
      e.printStackTrace();
    }
  }
} <font color="#009900">///:~ </PRE></font></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
argument to 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>main(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is now 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>final</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
since the anonymous inner class uses 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>args[0]</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
directly.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">This
shows you how anonymous inner classes allow the creation of quick-and-dirty
classes to solve problems. Since everything in Java revolves around classes,
this can be a useful coding technique. One benefit is that it keeps the code
that solves a particular problem isolated together in one spot. On the other
hand, it is not always as easy to read, so you must use it judiciously.
</FONT><P></DIV>
<A NAME="Heading314"></A><H4 ALIGN=LEFT>
A
sorted directory listing
</H4>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Ah,
you say that you want the file names 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>sorted</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">?
Since there&#8217;s no support for sorting in Java 1.0 or Java 1.1<A NAME="Index1074"></A>
(although sorting 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>is</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
included in Java 1.2<A NAME="Index1075"></A>),
it will have to be added into the program directly using the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>SortVector
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">created
in Chapter 8:
</FONT><P></DIV>

<font color="#990000"><PRE><font color="#009900">//: SortedDirList.java</font>
<font color="#009900">// Displays sorted directory listing</font>
<font color="#0000ff">import</font> java.io.*;
<font color="#0000ff">import</font> c08.*;

<font color="#0000ff">public</font> <font color="#0000ff">class</font> SortedDirList {
  <font color="#0000ff">private</font> File path;
  <font color="#0000ff">private</font> String[] list;
  <font color="#0000ff">public</font> SortedDirList(<font color="#0000ff">final</font> String afn) {
    path = <font color="#0000ff">new</font> File(".");
    <font color="#0000ff">if</font>(afn == <font color="#0000ff">null</font>)
      list = path.list();
    <font color="#0000ff">else</font>
      list = path.list(
          <font color="#0000ff">new</font> FilenameFilter() {
            <font color="#0000ff">public</font> <font color="#0000ff">boolean</font> 
            accept(File dir, String n) {
              String f = <font color="#0000ff">new</font> File(n).getName();
              <font color="#0000ff">return</font> f.indexOf(afn) != -1;

⌨️ 快捷键说明

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