wonderer's program

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

          2007年12月27日

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

          翻開《Java學習筆記》,里面對對象容器的描述不錯。

          1. ArrayList和LinkedList

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

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

          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進行遍歷
                  for(String s:list) {
                      //5.0的類C的輸出格式
                      System.out.printf("%s ",s);
                  }
              }
          }

          輸出:

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

           

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

          /**
           *
           * @author Rokey
           * 用LinkedList構(gòu)建一個字符棧,先進先出
           */
          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進行遍歷
                  
                  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 閱讀(3077) | 評論 (0)編輯 收藏

          2007年12月23日

          OYM中的任務中,有一項對文件內(nèi)容的檢查挺有意思的,就是要檢查字符是否是全角的,例如“GY”(not“GY”),并且把這些字符改為半角的。
          想起了在研發(fā)中心的一個朋友的抱怨:“昨天寫了一整天的程序,發(fā)到廣大教務處那邊居然說不能用,然后親自跑了一躺,發(fā)現(xiàn)不是我的程序有問題,是那邊的人輸入個全角字符,搜半角的字符,當然不行了”
          恩,Betty寫的需求真有意思,考慮的問題很周全,是一個很厲害的項目經(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 閱讀(1942) | 評論 (3)編輯 收藏

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

          今天想了一下,第一個要解決的問題就是上傳一個Excel文件,上傳文件的組件到挺多的,網(wǎng)上一搜,就有一大堆教程,但是現(xiàn)在并不是要上傳一個文件到服務器以作存儲之用,而是要上傳一個文件到內(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)編輯 收藏

          2007年10月28日

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

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

          2007年10月26日

          Buffloa里的傳遞參數(shù)的編碼是GBK。

          buffalo.switchPart('body',url,false);如果url中包含漢字,是采用GBK編碼的。在不改變tomcat的配置文件的情況下,在目標頁面里獲得url參數(shù)的正確方法是

             1: String name = new String(request.getParameter("name").getBytes(
             2:             "ISO8859-1"), "GBK");

          注意,如果這里用utf-8作為編碼的轉(zhuǎn)換的話,會出現(xiàn)亂碼。

          posted @ 2007-10-26 16:41 wonderer 閱讀(431) | 評論 (0)編輯 收藏

          2007年10月25日

          最近在準備考試系統(tǒng)的開發(fā),碰到了 request.getParameter亂碼的問題。跟林彬討論了一下,還是覺得用老方法管用。

          如果是post的話,可以通過設(shè)置filter的方法來解決。

          如果是get或者是超鏈接的話,以前是通過設(shè)置tomcat的配置文件server.xml來解決的,但這樣不好,并不是所有的項目,我們都可以修改到服務器的tomcat的配置文件。具體代碼如下:

             1: Connector port="8080" maxHttpHeaderSize="8192"
             2:                maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
             3:                enableLookups="false" redirectPort="8443" acceptCount="100"
             4:                connectionTimeout="20000" disableUploadTimeout="true" uRIEncoding="gbk"/>

          還是覺得老方法管用,只是有點麻煩:

             1: String id=new String(request.getParameter("id").getBytes("ISO8859-1"),"UTF-8");
             2: String name = new String(request.getParameter("name").getBytes("ISO8859-1"),"UTF-8");

          posted @ 2007-10-25 23:23 wonderer 閱讀(13434) | 評論 (1)編輯 收藏

          2007年9月11日

          電腦搬回了宿舍,破解了校園網(wǎng),多人公用一條寬帶。要一個主機撥號。ip總不免要設(shè)來設(shè)去,總是要手工改,很麻煩,于是上網(wǎng)查了查,寫了個改ip的bat文件。內(nèi)容如下:

          1: netsh interface ip set address name="本地連接" source=static addr=192.168.0.39

          mask=255.255.255.0 gateway=192.168.0.1 gwmetric=1

             2: netsh interface ip set dns name = "本地連接" source = static addr = 202.116.128.1
             3: netsh interface ip add dns name = "本地連接" addr = 202.116.128.2

          posted @ 2007-09-11 17:25 wonderer 閱讀(2062) | 評論 (1)編輯 收藏

          2007年8月3日

          在寫HTML中,并不是&nbsp才會產(chǎn)生一個空格。<td>hello (間隔一個空格)</td>輸出的數(shù)據(jù)是: hello+一個空格.如果是對這數(shù)據(jù)進行修改然后再寫回到數(shù)據(jù)庫的話,這樣就會產(chǎn)生錯誤。

          如下寫法是會錯誤的,

          image

          造成的結(jié)果是 image  仔細留意會發(fā)現(xiàn)運通后面多了一個空格

          必須改成一下寫法:

          image 

          注意</td>跟前面是沒有空格的。這樣運行結(jié)果就會是這樣的image ,是沒有空格的。

          posted @ 2007-08-03 10:32 wonderer 閱讀(675) | 評論 (0)編輯 收藏

          2007年7月18日

               摘要: 寫了個Spring的DAO入門例子。 DAO的接口 1: package dataSourceDemo; 2:   3: public interface IUserDAO { 4: public void insert(User user); 5: public User find(Integer id); 6:   7: } ...  閱讀全文

          posted @ 2007-07-18 14:04 wonderer 閱讀(496) | 評論 (0)編輯 收藏

          首先要導入包

          1:Spring支持包:spring.jar , commons-logging.jar

          2: JUnit支持包: JUnit.jar

          image

          建立Bean類,

             1: package refBeanDemo;
             2:  
             3: import java.util.Date;
             4:  
             5: public class HelloBean {
             6:     private String helloWorld;
             7:     private Date date;
             8:     public Date getDate() {
             9:         return date;
            10:     }
            11:     public void setDate(Date date) {
            12:         this.date = date;
            13:     }
            14:     public String getHelloWorld() {
            15:         return helloWorld;
            16:     }
            17:     public void setHelloWorld(String helloWorld) {
            18:         this.helloWorld = helloWorld;
            19:     }
            20:     
            21: }

           

          建立配置文件,和在里面進行注入

             1: <?xml version="1.0" encoding="UTF-8"?>
             2: <!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN" "../resources/spring-beans-2.0.dtd" >
             3: <beans>
             4:     <bean id="dateBean" class="java.util.Date"></bean>
             5:     
             6:     <bean id="helloBean" class="refBeanDemo.HelloBean">
             7:         <property name="helloWorld">
             8:             <value>你好,世界</value>
             9:         </property>
            10:         <property name="date" ref="dateBean"></property>
            11:     </bean>
            12: </beans>

          寫JUnit進行測試,方便管理,把JUnit的東東放到test包里。

             1: package refBeanDemo;
             2:  
             3: import org.springframework.context.ApplicationContext;
             4: import org.springframework.context.support.ClassPathXmlApplicationContext;
             5:  
             6: import junit.framework.TestCase;
             7:  
             8: public class TestRefBeanDemo extends TestCase {
             9:     private ApplicationContext context;
            10:  
            11:     public void setUp() {
            12:         context = new ClassPathXmlApplicationContext("refBeanDemo/NewFile.xml");
            13:     }
            14:  
            15:     public void testSpring() {
            16:         HelloBean helloBean = (HelloBean)context.getBean("helloBean");
            17:         System.out.println(helloBean.getDate());
            18:         assertEquals("你好,世界", helloBean.getHelloWorld());
            19:         
            20:     }
            21: }

           

          運行JUnit測試

          image

          測試成功。類的分布如下:

          image

          image

          posted @ 2007-07-18 11:12 wonderer 閱讀(1356) | 評論 (0)編輯 收藏

          主站蜘蛛池模板: 固镇县| 徐闻县| 靖江市| 临湘市| 文登市| 陕西省| 黎川县| 从化市| 合水县| 唐海县| 台东市| 凤城市| 永顺县| 遂溪县| 平阴县| 绥棱县| 宣汉县| 祁阳县| 南康市| 潞城市| 晋州市| 眉山市| 抚松县| 牙克石市| 甘孜县| 鹤壁市| 永清县| 砚山县| 海宁市| 武宣县| 辽阳县| 荆州市| 镇安县| 乌审旗| 吉安县| 嘉祥县| 萍乡市| 兴山县| 潮州市| 怀化市| 婺源县|