add java.util.Collection.toArray()

This commit is contained in:
Joel Dice 2009-08-04 17:36:25 -06:00
parent 7911989055
commit 0a7f94abfe
10 changed files with 40 additions and 19 deletions

View File

@ -62,6 +62,10 @@ public abstract class AbstractCollection<T> implements Collection<T> {
public abstract int size();
public Object[] toArray() {
return toArray(new Object[size()]);
}
public <S> S[] toArray(S[] array) {
return Collections.toArray(this, array);
}

View File

@ -159,10 +159,6 @@ public class ArrayList<T> extends AbstractList<T> {
return size() == 0;
}
public <S> S[] toArray(S[] a) {
return Collections.toArray(this, a);
}
public void clear() {
array = null;
size = 0;

View File

@ -81,6 +81,10 @@ public class Arrays {
throw new UnsupportedOperationException();
}
public Object[] toArray() {
return toArray(new Object[size()]);
}
public <S> S[] toArray(S[] a) {
return (S[])array;
}

View File

@ -23,6 +23,8 @@ public interface Collection<T> extends Iterable<T> {
public boolean remove(Object element);
public Object[] toArray();
public <S> S[] toArray(S[] array);
public void clear();

View File

@ -129,6 +129,10 @@ public class Collections {
synchronized (lock) { return collection.remove((T)e); }
}
public Object[] toArray() {
return toArray(new Object[size()]);
}
public <T> T[] toArray(T[] array) {
synchronized (lock) { return collection.toArray(array); }
}
@ -319,10 +323,13 @@ public class Collections {
return inner.size();
}
public Object[] toArray() {
return toArray(new Object[size()]);
}
public <S> S[] toArray(S[] array) {
return inner.toArray(array);
}
}
}
public static <T> Set<T> unmodifiableSet(Set<T> hs) {

View File

@ -334,6 +334,10 @@ public class HashMap<K, V> implements Map<K, V> {
return removeCell(e.getKey()) != null;
}
public Object[] toArray() {
return toArray(new Object[size()]);
}
public <T> T[] toArray(T[] array) {
return Collections.toArray(this, array);
}
@ -374,6 +378,10 @@ public class HashMap<K, V> implements Map<K, V> {
return removeCell(key) != null;
}
public Object[] toArray() {
return toArray(new Object[size()]);
}
public <T> T[] toArray(T[] array) {
return Collections.toArray(this, array);
}
@ -412,6 +420,10 @@ public class HashMap<K, V> implements Map<K, V> {
throw new UnsupportedOperationException();
}
public Object[] toArray() {
return toArray(new Object[size()]);
}
public <T> T[] toArray(T[] array) {
return Collections.toArray(this, array);
}

View File

@ -10,7 +10,7 @@
package java.util;
public class HashSet<T> implements Set<T> {
public class HashSet<T> extends AbstractSet<T> implements Set<T> {
private static final Object Value = new Object();
private final HashMap<T, Object> map;
@ -54,10 +54,6 @@ public class HashSet<T> implements Set<T> {
return map.remove(element) != Value;
}
public <T> T[] toArray(T[] array) {
return Collections.toArray(this, array);
}
public void clear() {
map.clear();
}

View File

@ -85,10 +85,6 @@ public class LinkedList<T> extends AbstractSequentialList<T> {
}
}
public <S> S[] toArray(S[] a) {
return Collections.toArray(this, a);
}
public int size() {
return size;
}

View File

@ -144,6 +144,10 @@ public class TreeMap<K,V> implements Map<K,V> {
return set.removeAndReturn(new MyEntry(key, null)) != null;
}
public Object[] toArray() {
return toArray(new Object[size()]);
}
public <T> T[] toArray(T[] array) {
return Collections.toArray(this, array);
}
@ -182,6 +186,10 @@ public class TreeMap<K,V> implements Map<K,V> {
throw new UnsupportedOperationException();
}
public Object[] toArray() {
return toArray(new Object[size()]);
}
public <T> T[] toArray(T[] array) {
return Collections.toArray(this, array);
}

View File

@ -77,10 +77,6 @@ public class Vector<T> extends AbstractList<T> {
return list.isEmpty();
}
public synchronized <S> S[] toArray(S[] a) {
return list.toArray(a);
}
public void removeElementAt(int index) {
remove(index);
}