码迷,mamicode.com
首页 > 其他好文 > 详细

String类(三)

时间:2015-11-09 17:17:37      阅读:264      评论:0      收藏:0      [点我收藏+]

标签:

构造函数

  •  public String(char value[], int offset, int count)

构造函数的源码

    public String(char value[], int offset, int count) {
        if (offset < 0) {
            throw new StringIndexOutOfBoundsException(offset);
        }
        if (count < 0) {
            throw new StringIndexOutOfBoundsException(count);
        }
        // Note: offset or count might be near -1>>>1.
        if (offset > value.length - count) {
            throw new StringIndexOutOfBoundsException(offset + count);
        }
        this.offset = 0;
        this.count = count;
        this.value = Arrays.copyOfRange(value, offset, offset+count);
    }

 String中的value[]中的值为传入数组中offset-(offset+count)索引指向的数组。

其中 public static char[] copyOfRange(char[] original, int from, int to)代码为:

    public static char[] copyOfRange(char[] original, int from, int to) {
        int newLength = to - from;
        if (newLength < 0)
            throw new IllegalArgumentException(from + " > " + to);
        char[] copy = new char[newLength];
        System.arraycopy(original, from, copy, 0,
                         Math.min(original.length - from, newLength));
        return copy;
    }

 数组copy函数。

String类(三)

标签:

原文地址:http://www.cnblogs.com/fckcmlf/p/4950261.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!