public class CharArrayList extends Object implements RandomAccess, Cloneable, Serializable, List<Character>, Comparable<List<? extends Character>>
This class implements a lightweight, fast, open, optimized, reuse-oriented version of array-based lists. Instances of this class represent a list with an array that is enlarged as needed when
new entries are created (by doubling its current length), but is never made smaller (even on a clear()). A family of trimming methods lets you control the size
of the backing array; this is particularly useful if you reuse instances of this class. Range checks are equivalent to those of java.util's classes, but they are delayed as much as
possible. The backing array is exposed by the elements() method.
This class implements the bulk methods removeElements(), addElements() and getElements() using high-performance system calls (e.g.,
System.arraycopy() instead of expensive loops.
ArrayList,
Serialized Form| Modifier and Type | Class and Description |
|---|---|
class |
CharArrayList.CharListIterator |
| Modifier | Constructor and Description |
|---|---|
protected |
CharArrayList(char[] a,
boolean dummy)
Creates a new array list using a given array.
|
|
CharArrayList(CharArrayList l)
Creates a new array list and fills it with a given type-specific list.
|
|
CharArrayList(int capacity)
Creates a new array list with given capacity.
|
|
CharArrayList(Iterator<? extends Character> i)
Creates a new array list and fills it with the elements returned by an iterator..
|
|
CharArrayList(String s)
Creates a new array list and fills it with the elements of a given String.
|
| Modifier and Type | Method and Description |
|---|---|
boolean |
add(Character o)
Delegates to the corresponding type-specific method.
|
boolean |
add(int k) |
void |
add(int index,
Character element)
Inserts the specified element at the specified position in this list
(optional operation).
|
boolean |
addAll(Collection<? extends Character> c)
Adds all elements of the given collection to this collection.
|
boolean |
addAll(int index,
CharArrayList l) |
boolean |
addAll(int index,
Collection<? extends Character> c) |
void |
addElements(int index,
char[] a,
int offset,
int length)
Adds elements to this type-specific list using optimized system calls.
|
void |
clear() |
CharArrayList |
clone() |
int |
compareTo(CharArrayList l)
Compares this array list to another array list.
|
int |
compareTo(List<? extends Character> l)
Compares this list to another object.
|
boolean |
contains(Object o)
Delegates to the corresponding type-specific method.
|
boolean |
containsAll(Collection<?> c)
Checks whether this collection contains all elements from the given collection.
|
char[] |
elements()
Returns the backing array of this list.
|
protected void |
ensureRestrictedIndex(int index)
Ensures that the given index is non-negative and smaller than the list size.
|
boolean |
equals(CharArrayList l)
Compares this type-specific array list to another one.
|
Character |
get(int index)
Deprecated.
Please use the corresponding type-specific method instead.
|
char |
getChar(int index) |
int |
getCodePoint(int index) |
int |
hashCode()
Returns the hash code for this list, which is identical to
List.hashCode(). |
int |
indexOf(Object o)
Returns the index of the first occurrence of the specified element
in this list, or -1 if this list does not contain the element.
|
boolean |
isEmpty() |
CharArrayList.CharListIterator |
iterator() |
int |
lastIndexOf(char k) |
int |
lastIndexOf(Object o)
Returns the index of the last occurrence of the specified element
in this list, or -1 if this list does not contain the element.
|
CharArrayList.CharListIterator |
listIterator() |
CharArrayList.CharListIterator |
listIterator(int index) |
Character |
remove(int index)
Removes the element at the specified position in this list (optional
operation).
|
boolean |
remove(Object ok)
Delegates to the type-specific
rem() method. |
boolean |
removeAll(Collection<?> c)
Removes from this list all of its elements that are contained in the
specified collection (optional operation).
|
void |
removeElements(int from,
int to)
Removes elements of this type-specific list using optimized system calls.
|
boolean |
retainAll(Collection<?> c)
Retains only the elements in this list that are contained in the
specified collection (optional operation).
|
Character |
set(int index,
Character ok)
Deprecated.
Please use the corresponding type-specific method instead.
|
int |
size() |
void |
size(int size) |
List<Character> |
subList(int fromIndex,
int toIndex)
Returns a view of the portion of this list between the specified
fromIndex, inclusive, and toIndex, exclusive.
|
Object[] |
toArray() |
char[] |
toArray(char[] a) |
Character[] |
toArray(Character[] a) |
<T> T[] |
toArray(T[] a)
Returns an array containing all of the elements in this list in
proper sequence (from first to last element); the runtime type of
the returned array is that of the specified array.
|
char[] |
toCharArray() |
String |
toString() |
void |
trim()
Trims this array list so that the capacity is equal to the size.
|
static int |
unwrap(CharArrayList.CharListIterator i,
char[] array)
Unwraps an iterator into an array starting at a given offset for a given number of elements.
|
static CharArrayList |
wrap(char[] a)
Wraps a given array into an array list.
|
equals, finalize, getClass, notify, notifyAll, wait, wait, waitequals, replaceAll, sort, spliteratorparallelStream, removeIf, streamprotected CharArrayList(char[] a, boolean dummy)
This constructor is only meant to be used by the wrapping methods.
a - the array that will be used to back this array list.public CharArrayList(int capacity)
capacity - the initial capacity of the array list (may be 0).public CharArrayList(CharArrayList l)
l - a type-specific list that will be used to fill the array list.public CharArrayList(String s)
s - a String whose char elements will be used to fill the array list.public CharArrayList(Iterator<? extends Character> i)
i - an iterator whose returned elements will fill the array list.public char[] elements()
public static CharArrayList wrap(char[] a)
Note it is guaranteed that the type of the array returned by elements() will be the same (see the comments in the class documentation).
a - an array to wrap.public boolean add(int k)
public char getChar(int index)
public int getCodePoint(int index)
public int lastIndexOf(char k)
@Deprecated public Character set(int index, Character ok)
public void add(int index, Character element)
add in interface List<Character>index - index at which the specified element is to be insertedelement - element to be insertedUnsupportedOperationException - if the add operation
is not supported by this listClassCastException - if the class of the specified element
prevents it from being added to this listNullPointerException - if the specified element is null and
this list does not permit null elementsIllegalArgumentException - if some property of the specified
element prevents it from being added to this listIndexOutOfBoundsException - if the index is out of range
(index < 0 || index > size())public Character remove(int index)
remove in interface List<Character>index - the index of the element to be removedUnsupportedOperationException - if the remove operation
is not supported by this listIndexOutOfBoundsException - if the index is out of range
(index < 0 || index >= size())public int indexOf(Object o)
indexOf in interface List<Character>o - element to search forClassCastException - if the type of the specified element
is incompatible with this list
(optional)NullPointerException - if the specified element is null and this
list does not permit null elements
(optional)public int lastIndexOf(Object o)
lastIndexOf in interface List<Character>o - element to search forClassCastException - if the type of the specified element
is incompatible with this list
(optional)NullPointerException - if the specified element is null and this
list does not permit null elements
(optional)public boolean addAll(int index, Collection<? extends Character> c)
public boolean removeAll(Collection<?> c)
removeAll in interface Collection<Character>removeAll in interface List<Character>c - collection containing elements to be removed from this listUnsupportedOperationException - if the removeAll operation
is not supported by this listClassCastException - if the class of an element of this list
is incompatible with the specified collection
(optional)NullPointerException - if this list contains a null element and the
specified collection does not permit null elements
(optional),
or if the specified collection is nullremove(Object),
contains(Object)public boolean retainAll(Collection<?> c)
retainAll in interface Collection<Character>retainAll in interface List<Character>c - collection containing elements to be retained in this listUnsupportedOperationException - if the retainAll operation
is not supported by this listClassCastException - if the class of an element of this list
is incompatible with the specified collection
(optional)NullPointerException - if this list contains a null element and the
specified collection does not permit null elements
(optional),
or if the specified collection is nullremove(Object),
contains(Object)public void clear()
public int size()
public void size(int size)
public boolean isEmpty()
public void trim()
ArrayList.trimToSize()public void removeElements(int from, int to)
from - the start index (inclusive).to - the end index (exclusive).public void addElements(int index, char[] a, int offset, int length)
index - the index at which to add elements.a - the array containing the elements.offset - the offset of the first element to add.length - the number of elements to add.public boolean addAll(int index, CharArrayList l)
public CharArrayList.CharListIterator iterator()
public CharArrayList.CharListIterator listIterator()
listIterator in interface List<Character>public CharArrayList.CharListIterator listIterator(int index)
listIterator in interface List<Character>public List<Character> subList(int fromIndex, int toIndex)
This method eliminates the need for explicit range operations (of the sort that commonly exist for arrays). Any operation that expects a list can be used as a range operation by passing a subList view instead of a whole list. For example, the following idiom removes a range of elements from a list:
list.subList(from, to).clear();
Similar idioms may be constructed for indexOf and
lastIndexOf, and all of the algorithms in the
Collections class can be applied to a subList.
The semantics of the list returned by this method become undefined if the backing list (i.e., this list) is structurally modified in any way other than via the returned list. (Structural modifications are those that change the size of this list, or otherwise perturb it in such a fashion that iterations in progress may yield incorrect results.)
subList in interface List<Character>fromIndex - low endpoint (inclusive) of the subListtoIndex - high endpoint (exclusive) of the subListIndexOutOfBoundsException - for an illegal endpoint index value
(fromIndex < 0 || toIndex > size ||
fromIndex > toIndex)public CharArrayList clone()
public boolean equals(CharArrayList l)
This method exists only for sake of efficiency. The implementation inherited from the abstract implementation would already work.
l - a type-specific array list.public int compareTo(CharArrayList l)
This method exists only for sake of efficiency. The implementation inherited from the abstract implementation would already work.
l - an array list.public int compareTo(List<? extends Character> l)
List, this method performs a lexicographical comparison; otherwise, it throws a ClassCastException.compareTo in interface Comparable<List<? extends Character>>l - a list.List, a negative integer, zero, or a positive integer as this list is lexicographically less than, equal to, or greater than the argument.ClassCastException - if the argument is not a list.public int hashCode()
List.hashCode().@Deprecated public Character get(int index)
protected void ensureRestrictedIndex(int index)
index - an index.IndexOutOfBoundsException - if the given index is negative or not smaller than the list size.public <T> T[] toArray(T[] a)
If the list fits in the specified array with room to spare (i.e., the array has more elements than the list), the element in the array immediately following the end of the list is set to null. (This is useful in determining the length of the list only if the caller knows that the list does not contain any null elements.)
Like the toArray() method, this method acts as bridge between
array-based and collection-based APIs. Further, this method allows
precise control over the runtime type of the output array, and may,
under certain circumstances, be used to save allocation costs.
Suppose x is a list known to contain only strings. The following code can be used to dump the list into a newly allocated array of String:
String[] y = x.toArray(new String[0]);
Note that toArray(new Object[0]) is identical in function to toArray().
toArray in interface Collection<Character>toArray in interface List<Character>a - the array into which the elements of this list are to
be stored, if it is big enough; otherwise, a new array of the
same runtime type is allocated for this purpose.ArrayStoreException - if the runtime type of the specified array
is not a supertype of the runtime type of every element in
this listNullPointerException - if the specified array is nullpublic boolean containsAll(Collection<?> c)
containsAll in interface Collection<Character>containsAll in interface List<Character>c - a collection.true if this collection contains all elements of the argument.public boolean addAll(Collection<? extends Character> c)
public char[] toArray(char[] a)
public char[] toCharArray()
public static int unwrap(CharArrayList.CharListIterator i, char[] array)
This method iterates over the given type-specific iterator and stores the elements returned, up to a maximum of length, in the given array starting at offset. The
number of actually unwrapped elements is returned (it may be less than max if the iterator emits less than max elements).
i - a type-specific iterator.array - an array to contain the output of the iterator.Copyright © 2016. All rights reserved.