Java Home

          Java技術修煉中...
          posts - 20, comments - 22, trackbacks - 0, articles - 0
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          數組對象getChars()方法的使用

          Posted on 2006-07-12 10:41 Yemoo'S Java Blog 閱讀(3098) 評論(2)  編輯  收藏 所屬分類: JAVA基礎知識
          java數組對象有一個很重要的方法getChars();其方法描述為:

          public void getChars(int srcBegin,int srcEnd,char dst[],int dstBegin);? 將當前字符串的部分字符復制到目標自負數組dst中,從srcBegin(包含在內)到srcEnd(不包含在內)之間的字符復制到目標字符數組中的字符從dstBegin位置開始存放.

          使用實例:
          String?ss="Visual?Basic";
          char?dst[]={'a','b','c','d'};

          ss.getChars(
          4,6,dst,2);
          System.out.println(dst);
          輸出結果為abal;

          注意數組參數只要寫入數組名即可,不要帶[],輸出可以直接用數組名輸出全部內容.

          評論

          # re: 數組對象getChars()方法的使用  回復  更多評論   

          2009-03-24 18:45 by anven
          你好,我剛剛在編寫代碼時候,出現了這樣的情況,在buf.getChars(0,buf.indexOf(new String ("<lesson")),tmp,0);時候,定義了一個tmp的char數組,這是循環使用這個數組,前后的數組成員的長度不同,因此在每一次的循環后,要求清空其中的內容,我使用一個
          for(int i=0,i<=100;i++)
          tmp[i]=0;
          這樣過后,卻出現不了預期的效果,卻只循環了一次就停止了!實在不知道錯在哪里!請教!
          import java.io.*;
          public class Filetest2
          {
          public static void main(String args[])
          {
          String str="";
          BufferedReader in =null;
          BufferedWriter out=null;
          char tmp[]=new char[50];
          int c=0;
          try
          {
          in = new BufferedReader(new FileReader("2.1.txt"));
          out= new BufferedWriter(new FileWriter("2.2.txt"));
          while((str=in.readLine())!=null)
          {
          String str1= str.toLowerCase().replaceAll(new String(" "),new String (""));
          StringBuffer buf= new StringBuffer(str1);
          System.out.println(buf.indexOf(new String ("<lesson")));
          if (buf.indexOf(new String ("<lesson"))!=-1)
          {
          buf.getChars(0,buf.indexOf(new String ("<lesson")),tmp,0);
          System.out.println(tmp);
          String Strtmp=new String(tmp);
          out.write(Strtmp);
          out.newLine();
          }

          // buf.getChars(0,buf.indexOf(new String ("<lesson"))-1,tmp,0);
          // System.out.println(tmp);
          //str1.getChars(str1.indexOf("*")+1,str1.indexOf("<"),tmp,0);
          //String Strtmp = new String(tmp);
          //System.out.println(str1);
          //out.write(str1);
          //out.newLine();
          }
          }
          catch(Exception e)
          {

          }
          finally
          {
          try{
          if(in!=null)
          if(out!=null)
          in.close();
          out.close();
          }
          catch(Exception e)
          {}

          }

          }

          }

          # re: 數組對象getChars()方法的使用  回復  更多評論   

          2011-05-05 20:50 by yx1989
          是因為你的數組長度和你getchars方法中獲得的字符串長度不一致,改一致了就可以了。

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 克拉玛依市| 南宫市| 五台县| 鸡泽县| 舞阳县| 明光市| 波密县| 灵石县| 淮滨县| 惠来县| 隆回县| 宝兴县| 屯昌县| 抚顺县| 诏安县| 边坝县| 泌阳县| 日喀则市| 红安县| 印江| 德州市| 莫力| 吴桥县| 湖北省| 普定县| 叶城县| 伊宁县| 杭锦后旗| 韶关市| 叙永县| 屯昌县| 安岳县| 马尔康县| 施甸县| 桃源县| 兰考县| 长治县| 陈巴尔虎旗| 社旗县| 汤原县| 靖边县|