<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    where the amazing happens

    算法1 : 遞歸

    一個(gè)簡(jiǎn)單的遞歸程序,它讀取給定目錄下所有文件,然后以樹型的方式在屏幕上打印出來.昨天斷斷續(xù)續(xù)得花了1個(gè)多小時(shí)寫出來

    讀取文件

    package ?test;
    import ?java.io. * ;
    import ?java.util. * ;

    /**
    ?*???Put?files?into?a?tree
    ?*?
    */

    public ? class ?ReadFilesAndBuiltTree? {
    ????
    ????
    static ? class ?Node {
    ????????
    private ?Node?father;
    ????????
    private ?List?children;
    ????????
    private ?File?data;
    ????????
    public ?Node() {
    ????????????children?
    = ? new ?Vector();
    ????????}

    ????????
    public ?File?getData()? {
    ????????????
    return ?data;
    ????????}

    ????????
    public ? void ?addChild(Node?c) {
    ????????????
    this .getChildren().add(c);
    ????????}

    ????????
    public ? void ?setData(File?data)? {
    ????????????
    this .data? = ?data;
    ????????}

    ????????
    public ?List?getChildren()? {
    ????????????
    return ?children;
    ????????}

    ????????
    public ? void ?setChildren(List?children)? {
    ????????????
    this .children? = ?children;
    ????????}

    ????????
    public ?Node?getFather()? {
    ????????????
    return ?father;
    ????????}

    ????????
    public ? void ?setFather(Node?father)? {
    ????????????
    this .father? = ?father;
    ????????}

    ????????
    public ? boolean ?hasChildren() {
    ????????????
    return ?children.size() > 0 ;
    ????????}

    ????????
    public ?Node?getChile( int ?index) {
    ????????????
    return ?(Node)children.get(index);
    ????????}

    ????????
    public ?Node?getChild(String?fname) {
    ????????????Iterator?i?
    = ?getChildren().iterator();
    ????????????
    while (i.hasNext()) {
    ????????????????Node?bn?
    = ?(Node)i.next();
    ????????????????String?n?
    = ?
    ????????????????????bn.getData().getName();
    ????????????????
    if (n.equals(fname)) return ?bn;
    ????????????}

    ????????????
    return ? null ;
    ????????}

    ????????
    public ?List?getDirChildren() {
    ????????????List?c?
    = ? new ?Vector();
    ????????????
    for ( int ?i? = ? 0 ?;?i? < children.size();i ++ ) {
    ????????????????Node?n?
    = ?(Node)children.get(i);
    ????????????????
    if (
    ??????????????????n.getData().isDirectory()????????
    ????????????????)c.add(n);
    ????????????}

    ????????????
    return ?c;
    ????????}

    ????????
    public ?List?getFileChildren() {
    ????????????List?c?
    = ? new ?Vector();
    ????????????
    for ( int ?i? = ? 0 ?;?i? < children.size();i ++ ) {
    ????????????????Node?n?
    = ?(Node)children.get(i);
    ????????????????
    if (
    ??????????????????
    ! n.getData().isDirectory()????????
    ????????????????)c.add(n);
    ????????????}

    ????????????
    return ?c;
    ????????}

    ????}

    ????
    ????
    private ?ReadFilesAndBuiltTree() {}
    ????
    ????
    public ? static ?Node?getTree(File?rootDir) {
    ????????Node?r?
    = ? new ?Node();
    ????????r.setData(rootDir);
    ????????
    return ? new ?ReadFilesAndBuiltTree().builtTree(r);
    ????}

    ????
    ????
    private ?Node?builtTree(Node?root) {
    ????????
    if ( ! root.getData().isDirectory()) return ?root;
    ????????File?f?
    = ?root.getData();
    ????????File[]?fs?
    = ?f.listFiles();
    ????????
    for ( int ?i? = ? 0 ?;?i? < ?fs.length?;?i ++ ?) {
    ????????????File?ff?
    = ?fs[i];
    ????????????Node?n?
    = ? new ?Node();
    ????????????n.setData(ff);
    ????????????n.setFather(root);
    ????????????root.addChild(builtTree(n));
    ????????}

    ????????
    return ?root;
    ????}

    ????
    ????
    public ? static ? void ?main(String[]?args) throws ?Exception {
    ????????Node?root?
    = ?getTree( new ?File(ShowFileDir.ROOT_DIR));
    ????????Node?cn?
    = ?root.getChild( " maps " );
    ????????System.out.println(cn.getChildren().size());
    ????}

    ????
    }


    輸出文件
    package?test;
    import?java.io.File;
    import?java.util.Iterator;
    import?java.util.List;

    import?test.ReadFilesAndBuiltTree.Node;

    /**
    ?*??Print?the?tree?to?screen
    ?*?
    */

    public?class?ShowFileDir?{
    ????
    ????
    public?static?String?ROOT_DIR?=?"E:/y音樂";
    ????
    ????
    public?ShowFileDir(){
    ????}

    ????
    ????
    public?void?printNodes(){
    ???????Node?root?
    =?ReadFilesAndBuiltTree.getTree(new?File(ROOT_DIR));
    ???????printNode(root,
    0);
    ????}

    ????
    ????
    private?void?printNode(Node?root,int?depth){
    ????????
    if(!root.hasChildren())return;
    ????????pd(root.getData().getName(),depth);
    ????????pindent(
    ++depth);
    ????????List?l?
    =?root.getFileChildren();
    ????????pc(l,depth);
    ????????l?
    =?root.getDirChildren();
    ????????
    for(int?i?=?0?;?i?<?l.size()?;?i++){
    ????????????Node?c?
    =?(Node)l.get(i);
    ????????????printNode(c,depth);
    ????????}

    ????}

    ????
    //?Screen?methods????
    ????private?void?pc(List?l,int?count){
    ????????Iterator?i?
    =?l.iterator();
    ????????
    while(i.hasNext()){
    ????????????Node?n?
    =?(Node)i.next();
    ????????????pp(count);
    ????????????p(
    "+"+n.getData().getName()+"\n");
    ????????}

    ????}

    ????
    ????
    private?void?pd(String?t,int?depth){
    ????????String?spc?
    =?"";
    ????????
    for(int?i?=?0;?i<?depth;?i++)spc+="-";
    ????????p(spc
    +"+"+t+"\n");
    ????}

    ????
    ????
    private?void?pindent(int?depth){
    ????????String?spc?
    =?"";
    ????????
    for(int?i?=?0;?i<?depth;?i++)spc+="?";
    ????????System.out.println(spc
    +"|");
    ????}

    ????
    ????
    private?void?pp(int?count){
    ????????
    for(int?i?=?0?;?i?<?count?;?i++)p("?");
    ????}

    ????
    ????
    private?void?p(String?s){System.out.print(s);}
    ????
    ????
    public?static?void?main(String[]?args)throws?Exception{
    ????????ShowFileDir?sfd?
    =?new?ShowFileDir();
    ????????sfd.printNodes();
    ????}

    }



    2個(gè)類,一個(gè)負(fù)責(zé)讀.一個(gè)負(fù)責(zé)寫.
    輸出結(jié)果:

    +y音樂
    ?|
    -+歐美
    ? |
    ? +WeAreTheWorld.txt
    ? +WeAreTheWorld.wmv
    ? +WhereAreYou!.txt
    ? +jeanny_3.mp3
    ? +blood-upon-the-risers.mp3
    ? +Broken Down Angel.txt
    ? +Love Hurts.txt
    ? +Broken Down Angle.mp3
    ? +Love Hurts.mp3
    ? +Celien Dion - God Bless America.mp3
    ? +SS閃電部隊(duì)在前進(jìn).mp3
    ? +The_Sun_Is_Burning_Simon_and_Garfunkel.mp3
    ? +vitas.-.opera02.rm
    ? +chivas we could be together.wma
    ? +綠色椅子.mp3
    ? +come away with me-norah jones.wma
    ? +the sound of silence.mp3
    --+live from new york city,1967-simon&garfunkel
    ?? |
    ?? +a church is burning.mp3
    ?? +a hazy shade of winter.mp3
    ?? +a most peculiar.mp3
    ?? +a poem on the underground wall.mp3
    ?? +anji.mp3
    ?? +benedictus.mp3
    ?? +blessed.mp3
    ?? +for emily,whenever I may find her.mp3
    ?? +he was my brother.mp3
    ?? +howeward bound.mp3
    ?? +I am a rock.mp3
    ?? +leaves that are green.mp3
    ?? +richard cory.mp3
    ?? +sparrow.mp3
    ?? +the 59th street bridge song.mp3
    ?? +the dangling conversation.mp3
    ?? +the sound of silence.mp3
    ?? +wednesday morning,3A.M.mp3
    ?? +you don't know where your interest lies.mp3
    -+其他
    ? |
    ? +StarCraft - Techno Mix.mp3
    ? +牽手.mp3
    ? +螢火蟲 .mp3
    ? +njsw.mp3
    ? +zeldaN64.rar


    這是算法系列的第一篇,下一個(gè)準(zhǔn)備寫動(dòng)態(tài)規(guī)劃的.

    posted on 2006-04-23 05:00 where the amazing happens 閱讀(899) 評(píng)論(1)  編輯  收藏 所屬分類: 算法&數(shù)據(jù)結(jié)構(gòu)

    評(píng)論

    # re: 算法1 : 遞歸[未登錄] 2008-01-20 13:10 無(wú)名

    private ReadFilesAndBuiltTree() {}
    你這里為什么要用private來定義構(gòu)造函數(shù)呢  回復(fù)  更多評(píng)論   


    只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


    網(wǎng)站導(dǎo)航:
     

    公告

    點(diǎn)擊這里給我發(fā)消息

    導(dǎo)航

    <2025年5月>
    27282930123
    45678910
    11121314151617
    18192021222324
    25262728293031
    1234567

    統(tǒng)計(jì)

    常用鏈接

    留言簿(3)

    隨筆分類(18)

    隨筆檔案(17)

    文章分類

    相冊(cè)

    其他我的blog

    技術(shù)Blog

    最新隨筆

    搜索

    最新評(píng)論

    閱讀排行榜

    評(píng)論排行榜

    主站蜘蛛池模板: 最近免费中文字幕中文高清| 亚洲乱码一二三四区麻豆| 亚洲国产主播精品极品网红| 国产一级大片免费看| 国产裸模视频免费区无码| 热99re久久精品精品免费| 欧美好看的免费电影在线观看| 国产精品色拉拉免费看| 18禁成人网站免费观看| 国产成人精品免费视频大| 青青视频观看免费99| 在线观看免费人成视频色9| 欧亚精品一区三区免费| 免费精品一区二区三区在线观看| 最近中文字幕无吗免费高清| 午夜小视频免费观看| 午夜视频免费观看| 四虎永久免费地址在线网站| 免费v片视频在线观看视频| 亚洲精品第一国产综合境外资源| 亚洲国模精品一区| 亚洲国产精品无码专区在线观看| 亚洲妇熟XXXX妇色黄| 一区二区三区亚洲| 亚洲国产成a人v在线观看| 亚洲av成本人无码网站| 一本岛v免费不卡一二三区| 好紧我太爽了视频免费国产| 中文字幕亚洲免费无线观看日本| 999国内精品永久免费视频| 永久久久免费浮力影院| 国产国拍亚洲精品福利 | 日本免费高清视频| 最近中文字幕电影大全免费版| 免费av欧美国产在钱| 免费亚洲视频在线观看| 亚洲AV无码一区二区乱子伦| 亚洲另类古典武侠| 无人视频在线观看免费播放影院| a级精品九九九大片免费看| 91网站免费观看|