Java Home

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

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

          Posted on 2006-07-12 10:41 Yemoo'S Java Blog 閱讀(3103) 評論(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方法中獲得的字符串長度不一致,改一致了就可以了。

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


          網站導航:
           
          主站蜘蛛池模板: 新丰县| 阿拉善左旗| 枞阳县| 寿阳县| 米林县| 吉首市| 浑源县| 嘉兴市| 化州市| 靖远县| 新源县| 绵竹市| 蒲江县| 磐安县| 汝州市| 西藏| 闻喜县| 宁明县| 时尚| 小金县| 安吉县| 肥城市| 同心县| 曲阳县| 边坝县| 朔州市| 遂昌县| 色达县| 雷山县| 三河市| 友谊县| 烟台市| 云霄县| 龙岩市| 新巴尔虎左旗| 阳泉市| 红河县| 海口市| 武义县| 大宁县| 定结县|