A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 林康春 黑马帝   /  2012-2-17 07:44  /  3600 人查看  /  11 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 林康春 于 2012-2-18 21:02 编辑

class  StringAPIDemo
{
        public static void main(String[] args)
        {
                String str1="hello";
                char c[]=str1.toCharArray();
                for (int i=0;i<c.length ; i++)
                {
                        System.out.print(c+"\t");
                }
                System.out.println("");
                String str2=new String(c);
                String str3=new String(c,0,3);
                System.out.println(str2);
                System.out.println(str3);

        }
}
输出运行结果:
h       e       l       l     o
hello
hel

疑问:String str3=new String(c,0,3);
这是什么意思?

评分

参与人数 1技术分 +1 收起 理由
唐秀启 + 1

查看全部评分

11 个回复

倒序浏览
public String(char[] value,
              int offset,
              int count)分配一个新的 String,它包含取自字符数组参数一个子数组的字符。offset 参数是子数组第一个字符的索引,count 参数指定子数组的长度。该子数组的内容已被复制;后续对字符数组的修改不会影响新创建的字符串。

参数:
value - 作为字符源的数组。
offset - 初始偏移量。
count - 长度。

String str3=new String(c,0,3);
表示从c数组截取一个子数组创建一个新的字符串,0表示从0角标位开始,3表示截取3个。

评分

参与人数 1技术分 +1 收起 理由
唐秀启 + 1

查看全部评分

回复 使用道具 举报
0-3取头不取尾,有很多方法都跟这个类似如substring...也就是说取的是0,1,2   这三个角标的元素组成字符串,就是hel
回复 使用道具 举报
String str3=new String(c,0,3);应该是截取数组c中0角标到2角标中的元素创建了一个新的字符串
回复 使用道具 举报
本帖最后由 pengfangjava 于 2012-2-17 09:07 编辑

楼主的问题可从JDK帮助文档中找到答案:
不知道楼主喜欢看中文的还是英文的,各来一份吧

public String(char[] value,
              int offset,
              int count)
Allocates a new String that contains characters from a subarray of the character array argument. The offset argument is the index of the first character of the subarray and the count argument specifies the length of the subarray. The contents of the subarray are copied; subsequent modification of the character array does not affect the newly created string.

Parameters:
value - Array that is the source of characters
offset - The initial offset
count - The length


public String(char[] value,
              int offset,
              int count)分配一个新的 String,它包含取自字符数组参数一个子数组的字符。offset 参数是子数组第一个字符的索引,count 参数指定子数组的长度。该子数组的内容已被复制;后续对字符数组的修改不会影响新创建的字符串。

参数:
value - 作为字符源的数组。
offset - 初始偏移量。
count - 长度。
抛出:
IndexOutOfBoundsException - 如果 offset 和 count 参数索引字符超出 value 数组的范围。

希望对你有帮助

评分

参与人数 1技术分 +1 收起 理由
唐秀启 + 1

查看全部评分

回复 使用道具 举报
String str2=new String(c);
这个是将全部字符数组变为String,它输出以后是hello
String str3=new String(c,0,3);
这个是将部分字符数组变为String,取字符数组c中从下标0开始,取3位,也就是hel
回复 使用道具 举报
王康 黑马帝 2012-2-17 09:27:15
7#
public class StringAPIDemo {
          public static void main(String[] args)
      {
              String str1="hello";        //String类型变量
              char c[]=str1.toCharArray();        //把字符串转换成字符数组
              for (int i=0;i<c.length ; i++)
              {
                      System.out.print(c+"\t");        //\t为制表符  h        e        l        l        o       
              }
              System.out.println("");        //换行
              String str2=new String(c);        //把数组转换成字符串
              String str3=new String(c,0,3);        //截取字符串str2,从下标0开始,截取3个字符长度的字符.
              System.out.println(str2);        //        hello
              System.out.println(str3);        //  hel
      }
}

评分

参与人数 1技术分 +1 收起 理由
祁焱 + 1 赞一个!

查看全部评分

回复 使用道具 举报
String str3=new String(c,0,3);
说明从字符数组的零脚标开始,获取3个字符组成一个字符串
回复 使用道具 举报
戚雪晖 黑马帝 2012-2-17 09:39:47
9#
1毫秒的价值 发表于 2012-2-17 08:23
0-3取头不取尾,有很多方法都跟这个类似如substring...也就是说取的是0,1,2   这三个角标的元素组成字符串 ...

我错了,通过查API
public String(byte[] bytes,int offset,int length)
参数:
bytes - 要解码为字符的 byte
offset - 要解码的第一个 byte 的索引
length - 要解码的 byte 数
回复 使用道具 举报
class  StringAPIDemo
{
        public static void main(String[] args)
        {
                String str1="hello";//创建字符串并赋初值为:hello
                char c[]=str1.toCharArray();//将字符串转化为字符数组,调用toCharArray方法
                for (int i=0;i<c.length ; i++)
                {
                        System.out.print(c[i]+"\t");//将字符数组打印
                }
                System.out.println("");//换行操作
                String str2=new String(c);//将字符数组转换为字符串
                String str3=new String(c,0,3);//从c字符串中取值,范围是从0角标开始,长度范围为4的子串
                System.out.println(str2);
                System.out.println(str3);

        }
}
回复 使用道具 举报
表示c从0开始取到第三个角标就是了《比如hello取出来就是hell,很简单,就相当 sub()
回复 使用道具 举报
明白了  谢谢
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马