Java Home

Java技术修炼中...
posts - 20, comments - 22, trackbacks - 0, articles - 0
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

数组对象getChars()方法的使用

Posted on 2006-07-12 10:41 Yemoo'S Java Blog 阅读(3086) 评论(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方法中获得的字符串长度不一致,改一致了就可以了。

只有注册用户登录后才能发表评论。


网站导航: