天天看點

String源代碼

java.lang.String

Point:

1、final修飾:該類不能被繼承;

2、實作接口:Serializable、Comparable、CharSequence(字元序列,子類有:String、StringBuilder、StringBuffer、CharBuffer、Segment)

3、以char[]形式存儲資料

屬性:

1、private char[] value; //字元存儲

2、private int hash; //哈希值

3、private ObjectStreamField[];//不明白其作用

構造方法:

1、空字元串

public String() {
        this.value = "".value;
    }
           

2、

public String(String original) {
        this.value = original.value;
        this.hash = original.hash;
    }
           

3、char數組

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

4、codePoints數組(我了解是unicode編碼數組)

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

        final int end = offset + count;

        // Pass 1: Compute precise size of char[]
        int n = count;
        for (int i = offset; i < end; i++) {
            int c = codePoints[i];
            if (Character.isBmpCodePoint(c))
                continue;
            else if (Character.isValidCodePoint(c))
                n++;
            else throw new IllegalArgumentException(Integer.toString(c));
        }

        // Pass 2: Allocate and fill in char[]
        final char[] v = new char[n];

        for (int i = offset, j = ; i < end; i++, j++) {
            int c = codePoints[i];
            if (Character.isBmpCodePoint(c))
                v[j] = (char)c;
            else
                Character.toSurrogates(c, v, j++);
        }

        this.value = v;
    }
           

常用方法:

1、length()

public int length() {
        return value.length;
    }
           

2、isEmpty()

public boolean isEmpty() {
        return value.length == ;
    }
           

3、CharAt()

public char charAt(int index) {
        if ((index < ) || (index >= value.length)) {
            throw new StringIndexOutOfBoundsException(index);
        }
        return value[index];
    }
           

4、getChars() //把String對象第srcBegin到srcEnd的字元串傳遞給dst字元數組,從第dstBegin開始

public void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin) {
        if (srcBegin < ) {
            throw new StringIndexOutOfBoundsException(srcBegin);
        }
        if (srcEnd > value.length) {
            throw new StringIndexOutOfBoundsException(srcEnd);
        }
        if (srcBegin > srcEnd) {
            throw new StringIndexOutOfBoundsException(srcEnd - srcBegin);
        }
        System.arraycopy(value, srcBegin, dst, dstBegin, srcEnd - srcBegin);
    }
           

5、equals()//周遊String中每個字元比較

public boolean equals(Object anObject) {
        if (this == anObject) {
            return true;
        }
        if (anObject instanceof String) {
            String anotherString = (String)anObject;
            int n = value.length;
            if (n == anotherString.value.length) {
                char v1[] = value;
                char v2[] = anotherString.value;
                int i = ;
                while (n-- != ) {
                    if (v1[i] != v2[i])
                        return false;
                    i++;
                }
                return true;
            }
        }
        return false;
    }
           

6、equalsIgnoreCase()

public boolean equalsIgnoreCase(String anotherString) {
        return (this == anotherString) ? true
                : (anotherString != null)
                && (anotherString.value.length == value.length)
                && regionMatches(true, , anotherString, , value.length);
    }
           

7、compareTo//String中每個字元依次比較(’-‘,char類型加減是unicode的加減,傳回int)

public int compareTo(String anotherString) {
        int len1 = value.length;
        int len2 = anotherString.value.length;
        int lim = Math.min(len1, len2);
        char v1[] = value;
        char v2[] = anotherString.value;

        int k = ;
        while (k < lim) {
            char c1 = v1[k];
            char c2 = v2[k];
            if (c1 != c2) {
                return c1 - c2;
            }
            k++;
        }
        return len1 - len2;
    }
           

8、startWith

public boolean startsWith(String prefix, int toffset) {
        char ta[] = value;
        int to = toffset;
        char pa[] = prefix.value;
        int po = ;
        int pc = prefix.value.length;
        // Note: toffset might be near -1>>>1.
        if ((toffset < ) || (toffset > value.length - pc)) {
            return false;
        }
        while (--pc >= ) {
            if (ta[to++] != pa[po++]) {
                return false;
            }
        }
        return true;
    }
           

9、hashCode//h = 31 * h + val[i] 不了解

public int hashCode() {
        int h = hash;
        if (h ==  && value.length > ) {
            char val[] = value;

            for (int i = ; i < value.length; i++) {
                h =  * h + val[i];
            }
            hash = h;
        }
        return h;
    }