📄 stringbuffer.java
字号:
System.arraycopy(value, index+1, value, index, count-index-1); count--; return this; } /** * Inserts the string representation of the <code>Object</code> * argument into this string buffer. * <p> * The second argument is converted to a string as if by the method * <code>String.valueOf</code>, and the characters of that * string are then inserted into this string buffer at the indicated * offset. * <p> * The offset argument must be greater than or equal to * <code>0</code>, and less than or equal to the length of this * string buffer. * * @param offset the offset. * @param obj an <code>Object</code>. * @return a reference to this <code>StringBuffer</code> object. * @exception StringIndexOutOfBoundsException if the offset is invalid. * @see java.lang.String#valueOf(java.lang.Object) * @see java.lang.StringBuffer#insert(int, java.lang.String) * @see java.lang.StringBuffer#length() */ public synchronized StringBuffer insert(int offset, Object obj) { return insert(offset, String.valueOf(obj)); } /** * Inserts the string into this string buffer. * <p> * The characters of the <code>String</code> argument are inserted, in * order, into this string buffer at the indicated offset, moving up any * characters originally above that position and increasing the length * of this string buffer by the length of the argument. If * <code>str</code> is <code>null</code>, then the four characters * <code>"null"</code> are inserted into this string buffer. * <p> * The character at index <i>k</i> in the new character sequence is * equal to: * <ul> * <li>the character at index <i>k</i> in the old character sequence, if * <i>k</i> is less than <code>offset</code> * <li>the character at index <i>k</i><code>-offset</code> in the * argument <code>str</code>, if <i>k</i> is not less than * <code>offset</code> but is less than <code>offset+str.length()</code> * <li>the character at index <i>k</i><code>-str.length()</code> in the * old character sequence, if <i>k</i> is not less than * <code>offset+str.length()</code> * </ul><p> * The offset argument must be greater than or equal to * <code>0</code>, and less than or equal to the length of this * string buffer. * * @param offset the offset. * @param str a string. * @return a reference to this <code>StringBuffer</code> object. * @exception StringIndexOutOfBoundsException if the offset is invalid. * @see java.lang.StringBuffer#length() */ public synchronized StringBuffer insert(int offset, String str) { if ((offset < 0) || (offset > count)) { throw new StringIndexOutOfBoundsException(); } if (str == null) { str = String.valueOf(str); } int len = str.length(); int newcount = count + len; if (newcount > value.length) expandCapacity(newcount); else if (shared) copy(); System.arraycopy(value, offset, value, offset + len, count - offset); str.getChars(0, len, value, offset); count = newcount; return this; } /** * Inserts the string representation of the <code>char</code> array * argument into this string buffer. * <p> * The characters of the array argument are inserted into the * contents of this string buffer at the position indicated by * <code>offset</code>. The length of this string buffer increases by * the length of the argument. * <p> * The overall effect is exactly as if the argument were converted to * a string by the method {@link String#valueOf(char[])} and the * characters of that string were then * {@link #insert(int,String) inserted} into this * <code>StringBuffer</code> object at the position indicated by * <code>offset</code>. * * @param offset the offset. * @param str a character array. * @return a reference to this <code>StringBuffer</code> object. * @exception StringIndexOutOfBoundsException if the offset is invalid. */ public synchronized StringBuffer insert(int offset, char str[]) { if ((offset < 0) || (offset > count)) { throw new StringIndexOutOfBoundsException(); } int len = str.length; int newcount = count + len; if (newcount > value.length) expandCapacity(newcount); else if (shared) copy(); System.arraycopy(value, offset, value, offset + len, count - offset); System.arraycopy(str, 0, value, offset, len); count = newcount; return this; } /** * Inserts the string representation of the <code>boolean</code> * argument into this string buffer. * <p> * The second argument is converted to a string as if by the method * <code>String.valueOf</code>, and the characters of that * string are then inserted into this string buffer at the indicated * offset. * <p> * The offset argument must be greater than or equal to * <code>0</code>, and less than or equal to the length of this * string buffer. * * @param offset the offset. * @param b a <code>boolean</code>. * @return a reference to this <code>StringBuffer</code> object. * @exception StringIndexOutOfBoundsException if the offset is invalid. * @see java.lang.String#valueOf(boolean) * @see java.lang.StringBuffer#insert(int, java.lang.String) * @see java.lang.StringBuffer#length() */ public StringBuffer insert(int offset, boolean b) { return insert(offset, String.valueOf(b)); } /** * Inserts the string representation of the <code>char</code> * argument into this string buffer. * <p> * The second argument is inserted into the contents of this string * buffer at the position indicated by <code>offset</code>. The length * of this string buffer increases by one. * <p> * The overall effect is exactly as if the argument were converted to * a string by the method {@link String#valueOf(char)} and the character * in that string were then {@link #insert(int, String) inserted} into * this <code>StringBuffer</code> object at the position indicated by * <code>offset</code>. * <p> * The offset argument must be greater than or equal to * <code>0</code>, and less than or equal to the length of this * string buffer. * * @param offset the offset. * @param c a <code>char</code>. * @return a reference to this <code>StringBuffer</code> object. * @exception IndexOutOfBoundsException if the offset is invalid. * @see java.lang.StringBuffer#length() */ public synchronized StringBuffer insert(int offset, char c) { int newcount = count + 1; if (newcount > value.length) expandCapacity(newcount); else if (shared) copy(); System.arraycopy(value, offset, value, offset + 1, count - offset); value[offset] = c; count = newcount; return this; } /** * Inserts the string representation of the second <code>int</code> * argument into this string buffer. * <p> * The second argument is converted to a string as if by the method * <code>String.valueOf</code>, and the characters of that * string are then inserted into this string buffer at the indicated * offset. * <p> * The offset argument must be greater than or equal to * <code>0</code>, and less than or equal to the length of this * string buffer. * * @param offset the offset. * @param i an <code>int</code>. * @return a reference to this <code>StringBuffer</code> object. * @exception StringIndexOutOfBoundsException if the offset is invalid. * @see java.lang.String#valueOf(int) * @see java.lang.StringBuffer#insert(int, java.lang.String) * @see java.lang.StringBuffer#length() */ public StringBuffer insert(int offset, int i) { return insert(offset, String.valueOf(i)); } /** * Inserts the string representation of the <code>long</code> * argument into this string buffer. * <p> * The second argument is converted to a string as if by the method * <code>String.valueOf</code>, and the characters of that * string are then inserted into this string buffer at the position * indicated by <code>offset</code>. * <p> * The offset argument must be greater than or equal to * <code>0</code>, and less than or equal to the length of this * string buffer. * * @param offset the offset. * @param l a <code>long</code>. * @return a reference to this <code>StringBuffer</code> object. * @exception StringIndexOutOfBoundsException if the offset is invalid. * @see java.lang.String#valueOf(long) * @see java.lang.StringBuffer#insert(int, java.lang.String) * @see java.lang.StringBuffer#length() */ public StringBuffer insert(int offset, long l) { return insert(offset, String.valueOf(l)); } /** * The character sequence contained in this string buffer is * replaced by the reverse of the sequence. * <p> * Let <i>n</i> be the length of the old character sequence, the one * contained in the string buffer just prior to execution of the * <code>reverse</code> method. Then the character at index <i>k</i> in * the new character sequence is equal to the character at index * <i>n-k-1</i> in the old character sequence. * * @return a reference to this <code>StringBuffer</code> object.. * @since JDK1.0.2 */ public synchronized StringBuffer reverse() { if (shared) copy(); int n = count - 1; for (int j = (n-1) >> 1; j >= 0; --j) { char temp = value[j]; value[j] = value[n - j]; value[n - j] = temp; } return this; } /** * Converts to a string representing the data in this string buffer. * A new <code>String</code> object is allocated and initialized to * contain the character sequence currently represented by this * string buffer. This <code>String</code> is then returned. Subsequent * changes to the string buffer do not affect the contents of the * <code>String</code>. * <p> * Implementation advice: This method can be coded so as to create a new * <code>String</code> object without allocating new memory to hold a * copy of the character sequence. Instead, the string can share the * memory used by the string buffer. Any subsequent operation that alters * the content or capacity of the string buffer must then make a copy of * the internal buffer at that time. This strategy is effective for * reducing the amount of memory allocated by a string concatenation * operation when it is implemented using a string buffer. * * @return a string representation of the string buffer. */ public native String toString();/****** * public String toString() { * return new String(this); * } ******/ // // The following two methods are needed by String to efficiently // convert a StringBuffer into a String. They are not public. // They shouldn't be called by anyone but String. final void setShared() { shared = true; } final char[] getValue() { return value; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -