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

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

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

    wonderer's program

    everything will be better
    posts - 19, comments - 6, trackbacks - 0, articles - 0
      BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

    2007年10月28日

    去SA面試的時候,面試官問我平時用Java的什么數(shù)據(jù)結(jié)構(gòu),答曰:Vector。又問:哪有用過其他的的嗎?例如List和Map之類的。答曰:甚少。(自己汗一個,沒水平)既然不會就要學(xué)習(xí)啦。

    翻開《Java學(xué)習(xí)筆記》,里面對對象容器的描述不錯。

    1. ArrayList和LinkedList

    ArrayList使用了數(shù)組結(jié)構(gòu)實(shí)現(xiàn)List的數(shù)據(jù)。所以ArraryList用來快速定位對象是非常有效率的。但是如果要對ArraryList中間插入或者刪除,效率會非常低。

    LinkedList使用鏈表來實(shí)現(xiàn)的List。所以跟ArrayList相反,LinkedList對于插入和刪除是非常有優(yōu)勢,反之對于快速定位,是LinkedList的弱項(xiàng)。

    1)ArrayListDemo

    public class ArrayListDemo {
        public static void main(String[] args) {
            
            //用Scanner類,可以輕松獲得commander的輸入
            Scanner scanner = new Scanner(System.in);
            
            List<String> list = new ArrayList<String>();
            
            //在控制臺輸入,quit退出
            while(true) {
                System.out.print("Rokey@console# ");
                String input = scanner.next();
                if(input.equals("quit")) {
                    break;
                }
                list.add(input);
            }
            
            System.out.print("顯示輸入:");
            
            //使用5.0的foreach功能對List進(jìn)行遍歷
            for(String s:list) {
                //5.0的類C的輸出格式
                System.out.printf("%s ",s);
            }
        }
    }

    輸出:

    Rokey@console# 一二三
    Rokey@console# 三二一
    Rokey@console# quit
    顯示輸入:一二三 三二一 
    

     

    2)用LinkedList實(shí)現(xiàn)的一個字符串棧

    /**
     *
     * @author Rokey
     * 用LinkedList構(gòu)建一個字符棧,先進(jìn)先出
     */
    public class StringStack {
    
        private LinkedList<String> linkList;
    
        public StringStack() {
            linkList = new LinkedList<String>();
        }
    
        public void push(String s) {
            //將元素加入鏈表第一個位置
            linkList.addFirst(s);
        }
    
        public String pop() {
            //刪除鏈表第一個元素,并返回
            return linkList.removeFirst();
        }
    
        public String top() {
            //返回鏈表第一個元素,但并不刪除
            return linkList.getFirst();
        }
    
        public boolean isEmpty() {
            //檢查鏈表是否為空
            return linkList.isEmpty();
        }
    }
    public class StringStackDemo {
    
        public static void main(String[] args) {
    
            //用Scanner類,可以輕松獲得commander的輸入
            Scanner scanner = new Scanner(System.in);
    
            StringStack stack = new StringStack();
    
            //在控制臺輸入,quit退出
            while (true) {
                System.out.print("Rokey@console# ");
                String input = scanner.next();
                if (input.equals("quit")) {
                    break;
                }
                stack.push(input);
            }
    
            System.out.print("顯示輸入:");
            //使用5.0的foreach功能對List進(jìn)行遍歷
            
            while(!stack.isEmpty()) {
                //5.0的類C的輸出格式
                System.out.printf("%s ", stack.pop());
            }
        }
    }

    輸出:

    Rokey@console# 一二三
    Rokey@console# 三二一
    Rokey@console# quit
    顯示輸入:三二一 一二三 

    posted @ 2007-12-27 23:05 wonderer 閱讀(3078) | 評論 (0)編輯 收藏

    OYM中的任務(wù)中,有一項(xiàng)對文件內(nèi)容的檢查挺有意思的,就是要檢查字符是否是全角的,例如“GY”(not“GY”),并且把這些字符改為半角的。
    想起了在研發(fā)中心的一個朋友的抱怨:“昨天寫了一整天的程序,發(fā)到廣大教務(wù)處那邊居然說不能用,然后親自跑了一躺,發(fā)現(xiàn)不是我的程序有問題,是那邊的人輸入個全角字符,搜半角的字符,當(dāng)然不行了”
    恩,Betty寫的需求真有意思,考慮的問題很周全,是一個很厲害的項(xiàng)目經(jīng)理。如果從輸入這里解決了字符是否是半角的,那么,以后的情況就容易解決很多了。恩,網(wǎng)上搜了一下資料,查了一下書,得出了以下代碼:
    public void testChar() {
      String s1 
    = "123";
      String s2 
    = "abc";
      String s3 
    = "123abc";
      System.out.println(s1);
      System.out.println(s2);
      System.out.println(s3);
      
    for (int i = 0; i < s1.length(); i++) {
       
    int j = s1.charAt(i);
       
    if (j > 256) {
        
    int temp = j - 65248;
        
    if (temp >= 0) {
         System.out.print((
    char)j+"-->:" + (char) temp);
        } 
    else {
          System.out.print((
    char) j);
        }
       } 
    else {
        System.out.print((
    char) j);
       }
      }
      System.out.println();
      
      
    for (int i = 0; i < s2.length(); i++) {
       
    int j = s2.charAt(i);
       
    if (j > 256) {
        
    int temp = j - 65248;
        
    if (temp >= 0) {
         System.out.print((
    char)j+"-->:" + (char) temp);
        } 
    else {
         System.out.print((
    char) j);
        }
       } 
    else {
        System.out.print ((
    char) j);
       }
      }
      System.out.println();
      
      
    for (int i = 0; i < s3.length(); i++) {
       
    int j = s3.charAt(i);
       
    if (j > 256) {
        
    int temp = j - 65248;
        
    if (temp >= 0) {
          System.out.print((
    char)j+"-->:" + (char) temp);
        } 
    else {
         System.out.print((
    char) j);
        }
       } 
    else {
        System.out.print((
    char) j);
       }
      }
      System.out.println();
     
     }
    輸出的結(jié)果如下:
    123
    -->ab-->bc--c
    123a
    -->ab-->bc--c


    posted @ 2007-12-23 16:46 wonderer 閱讀(1943) | 評論 (3)編輯 收藏

    OYM的任務(wù)中,有個要求,上傳一個Excel文件,檢查他的內(nèi)容是否合法,并返回信息。

    今天想了一下,第一個要解決的問題就是上傳一個Excel文件,上傳文件的組件到挺多的,網(wǎng)上一搜,就有一大堆教程,但是現(xiàn)在并不是要上傳一個文件到服務(wù)器以作存儲之用,而是要上傳一個文件到內(nèi)存里,以Java的數(shù)據(jù)結(jié)構(gòu)存儲起來,并檢查,把合乎要求的數(shù)據(jù)寫到數(shù)據(jù)庫里。所以在網(wǎng)上的一大堆上傳文件的組件并不合用。于是又想自己寫,思路就是從客戶端那里獲取一個InputStream,然后就對這個InputStream做一系列的檢查。代碼如下:

    ServletInputStream sis =  request.getInputStream();
    InputStreamReader isr = new InputStreamReader(sis);
                 
    int ch;
    while((ch = isr.read()) != -1 ) {          
       out.println((char)ch);
    }
                 
    System.out.flush();

    結(jié)果的出去就是如下(輸出東西寫到頁面):

    -----------------------------7d7ea23120550 
    Content-Disposition: form-data; name="file1"; 
    filename="C:\Documents and Settings\Administrator\桌面\test.txt" 
    Content-Type: text/plain 
    my name is Rokey.Rokey。我的名字叫Rokey. 
    -----------------------------7d7ea23120550 Content-Disposition: form-data; 
    name="Submit" 上傳 -----------------------------7d7ea23120550--
    很明顯,這里只有
    my name is Rokey.Rokey。我的名字叫Rokey.

    對我有用,這個也正是我的文件里面的內(nèi)容,其它的都是關(guān)于這些form的其它信息。對我這個程序是沒有用的。如果這里寫下去的話,還要我去分析那些是數(shù)據(jù),哪些是form的參數(shù)。好,到現(xiàn)在為止,我已經(jīng)打消了自己寫的念頭了。我想,那些組件都可以把上傳文件封裝得那么好,能不能利用那些庫,抽出文件的IO流,讓我操作呢?

    于是,就開始對的API看,看到里面有這么一段。

    public class MultipartParser
    extends java.lang.Object
    A utility class to handle multipart/form-data requests, the kind of requests that support file uploads. This class uses a "pull" model where the reading of incoming files and parameters is controlled by the client code, which allows incoming files to be stored into any OutputStream. If you wish to use an API which resembles HttpServletRequest, use the "push" model MultipartRequest instead. It's an easy-to-use wrapper around this class.

    This class can receive arbitrarily large files (up to an artificial limit you can set), and fairly efficiently too. It cannot handle nested data (multipart content within multipart content). It can now with the latest release handle internationalized content (such as non Latin-1 filenames).

    It also optionally includes enhanced buffering and Content-Length limitation. Buffering is only required if your servlet container is poorly implemented (many are, including Tomcat 3.2), but it is generally recommended because it will make a slow servlet container a lot faster, and will only make a fast servlet container a little slower. Content-Length limiting is usually only required if you find that your servlet is hanging trying to read the input stram from the POST, and it is similarly recommended because it only has a minimal impact on performance.

    而且里面的API已經(jīng)封裝程我想象得到的情況了。于是,我就覺得這樣我就可以完成我的功能了。于是,就寫了以下代碼:

    MultipartParser mp = new MultipartParser(request, 10 * 1024 * 1024);
    Part part;
    while ((part = mp.readNextPart()) != null) {
          if (part.isParam()) {
              // it's a parameter part
              ParamPart paramPart = (ParamPart) part;
              //out.println("param: name=" + name + "; value=" + value);
          } else if (part.isFile()) {
              FilePart filePart = (FilePart) part;
              InputStream is = filePart.getInputStream();
              InputStreamReader isr = new InputStreamReader(is);
    
              int ch;
              while ((ch = isr.read()) != -1) {
    
                  out.print((char) ch);
              }
    
              System.out.flush();
              isr.close();
              is.close();
          }
    }
                   

    出去結(jié)果如下:

    my name is Rokey.Rokey。
    我的名字叫Rokey.
    到現(xiàn)在,已經(jīng)可以把這個流封裝成一個文件流,送給Excel的組件去處理了。

    posted @ 2007-12-23 00:52 wonderer 閱讀(1446) | 評論 (0)編輯 收藏

         摘要: 什么是IOC呢,在網(wǎng)上搜到了一非常有意思的講解。IoC就是Inversion of Control,控制反轉(zhuǎn)。在Java開發(fā)中,IoC意味著將你設(shè)計(jì)好的類交給系統(tǒng)去控制,而不是在你的類內(nèi)部控制。這稱為控制反轉(zhuǎn)。 下面我們以幾個例子來說明什么是IoC 假設(shè)我們要設(shè)計(jì)一個Girl和一個Boy類,其中Girl有kiss方法,即Girl想要Kiss一個Boy。那么,我們的問題是,Girl如何能夠認(rèn)識這個B...  閱讀全文

    posted @ 2007-10-28 16:50 wonderer 閱讀(685) | 評論 (0)編輯 收藏

    主站蜘蛛池模板: 亚洲国产无套无码av电影| 亚洲情A成黄在线观看动漫软件 | 亚洲人成毛片线播放| 成年女人免费视频播放体验区| 久久久久久久久无码精品亚洲日韩| 丁香五月亚洲综合深深爱| 91精品国产免费久久国语麻豆| 国产亚洲精aa在线看| 国产亚洲一区区二区在线| 亚洲视频免费观看| 特黄特色大片免费| 亚洲日本香蕉视频| 亚洲性在线看高清h片| 日韩一区二区a片免费观看 | 中文字幕不卡免费高清视频| 色噜噜亚洲男人的天堂| 亚洲欧洲日产国码av系列天堂| 免费无码又黄又爽又刺激| a级毛片毛片免费观看永久| 亚洲精品乱码久久久久蜜桃 | 久久国产乱子伦精品免费强| 亚洲精品GV天堂无码男同| 久久夜色精品国产嚕嚕亚洲av| 国产老女人精品免费视频| 91香焦国产线观看看免费 | 免费可以看黄的视频s色| 久久www免费人成精品香蕉| 亚洲欧洲无卡二区视頻| 亚洲日本精品一区二区| 亚洲精品偷拍视频免费观看| 日韩精品无码区免费专区| 午夜不卡久久精品无码免费| 成人国产网站v片免费观看| 精品国产日韩久久亚洲| 久久久国产精品亚洲一区| 亚洲伊人色欲综合网| avtt亚洲天堂| 国产老女人精品免费视频| 啦啦啦中文在线观看电视剧免费版| 久久精品无码专区免费青青| 免费看成人AA片无码视频吃奶|