DynamicCharacterArraypublic class DynamicCharacterArray extends Object Similiar to StringBuffer but does NOT resize when needed, only when
requested |
Fields Summary |
---|
protected char[] | bufferbuffer to store data. The capacity of this DynamicCharacterArray
is equal to the length of this buffer | protected int | lengththe length of the array currently in use |
Constructors Summary |
---|
public DynamicCharacterArray(int capacity)Initializes the DCA with a capacity and an empty array
this(null, capacity);
| public DynamicCharacterArray(String str)Initializes the array with str and a capacity of str.length()
this(str.toCharArray());
| public DynamicCharacterArray(char[] data)Initializes the array with data and a capacity of data.length
this(data, data.length);
| public DynamicCharacterArray(char[] data, int capacity)Initializes the array with data and a capacity of capacity
int len;
if (capacity <= 0) {
throw new IllegalArgumentException();
}
if (data != null) {
if (data.length > capacity) {
throw new IllegalArgumentException();
}
this.length = data.length;
buffer = new char[capacity];
System.arraycopy(data, 0, buffer, 0, this.length);
} else {
buffer = new char[capacity];
}
|
Methods Summary |
---|
public void | append(char c)Appends a character onto the end of the buffer.
insert(length, c);
| public int | capacity()Returns the current capacity
return buffer.length;
| public char | charAt(int index)Returns the character at the specified index of the internal buffer
if (index < 0 || index > this.length) {
throw new IndexOutOfBoundsException();
}
return buffer[index];
| public void | delete(int offset, int length)Deletes the sepecified range from the internal buffer
if (offset < 0
|| length < 0
|| (offset + length) < 0
|| (offset + length) > this.length) {
throw new StringIndexOutOfBoundsException();
}
if ((offset + length) < this.length) {
System.arraycopy(buffer, offset + length,
buffer, offset,
this.length - (offset + length));
}
this.length -= length;
| public void | get(char[] data)Returns the internal buffer in the array provided. This must
be at least length() long.
getChars(0, buffer.length, data, 0);
| public void | getChars(int position, int length, char[] data, int offset)Returns the internal buffer in the array provided.
System.arraycopy(buffer, position, data, offset, length);
| public int | insert(char[] data, int offset, int length, int position)Inserts an subset of an array into the buffer.
The offset parameter must be withint the range [0..(size())], inclusive.
The length parameter must be a non-negative integer such that
(offset + length) <= size().
if (position < 0) {
position = 0;
} else if (position > this.length) {
position = this.length;
}
if (offset < 0
|| offset > data.length
|| length < 0
|| length > data.length
|| (offset + length) < 0
|| (offset + length) > data.length) {
throw new ArrayIndexOutOfBoundsException();
}
if (this.length + length > buffer.length) {
throw new IllegalArgumentException();
}
if (data.length != 0) {
System.arraycopy(buffer, position,
buffer, position + length,
this.length - position);
System.arraycopy(data, offset, buffer, position, length);
this.length += length;
}
return position;
| public int | insert(int position, char ch)Inserts an character into the buffer.
char arr[] = { ch };
return insert(arr, 0, 1, position);
| public int | insert(int position, java.lang.String str)Inserts an String into the buffer.
return insert(str.toCharArray(), 0, str.length(), position);
| public int | length()Returns the length of current data
return this.length;
| public void | set(char[] data, int offset, int length)Sets the internal buffer to the values of the subset specified
The offset parameter must be withint the range [0..(size())], inclusive.
The length parameter must be a non-negative integer such that
(offset + length) <= size(). If data is null the buffer is emptied.
if (data == null) {
this.length = 0;
return;
}
if (offset < 0
|| offset > data.length
|| length < 0
|| length > data.length
|| (offset + length) < 0
|| (offset + length) > data.length) {
throw new ArrayIndexOutOfBoundsException();
}
if (length > buffer.length) {
throw new IllegalArgumentException();
}
System.arraycopy(data, offset, buffer, 0, length);
this.length = length;
| public void | setCapacity(int capacity)Sets the maximum capacity to the specified value. the buffer may
be truncated if the new capacity is less than the current capacity.
if (capacity <= 0) {
throw new IllegalArgumentException();
}
if (buffer.length == capacity) {
return;
}
if (this.length > capacity) {
this.length = capacity;
}
char[] newBuffer = new char[capacity];
System.arraycopy(buffer, 0, newBuffer, 0, this.length);
buffer = newBuffer;
| public void | setCharAt(int index, char ch)Sets the character at index to ch
if (index < 0 || index > this.length) {
throw new IndexOutOfBoundsException();
}
buffer[index] = ch;
| public char[] | toCharArray()Returns a copy of the active portion of the internal buffer.
char[] buf = new char[length];
System.arraycopy(buffer, 0, buf, 0, buf.length);
return buf;
| public java.lang.String | toString()Return a String representation of the internal buffer
return String.valueOf(buffer, 0, this.length);
|
|