diff --git a/classpath/java/util/ArrayList.java b/classpath/java/util/ArrayList.java index 0bd90b481c..00482f5722 100644 --- a/classpath/java/util/ArrayList.java +++ b/classpath/java/util/ArrayList.java @@ -24,11 +24,9 @@ public class ArrayList implements List { this(0); } - public ArrayList(Collection source) { + public ArrayList(Collection source) { this(source.size()); - for (T o : source) { - add(o); - } + addAll(source); } private void grow() { @@ -162,20 +160,7 @@ public class ArrayList implements List { } public S[] toArray(S[] a) { - Object[] retVal = null; - - if (a.length >= size) { - retVal = a; - } else { - retVal = new Object[size]; - } - for (int i = 0; i < size; ++i) { - retVal[i] = array[i]; - } - if (a.length > size) { - a[size] = null; - } - return (S[])retVal; + return Collections.toArray(this, a); } public void clear() { diff --git a/classpath/java/util/Collection.java b/classpath/java/util/Collection.java index bfa3ae64b7..cfde456aab 100644 --- a/classpath/java/util/Collection.java +++ b/classpath/java/util/Collection.java @@ -23,7 +23,7 @@ public interface Collection extends Iterable { public boolean remove(T element); - public T[] toArray(T[] array); + public S[] toArray(S[] array); public void clear(); } diff --git a/classpath/java/util/HashSet.java b/classpath/java/util/HashSet.java index d541dc78cc..690dc7cd3c 100644 --- a/classpath/java/util/HashSet.java +++ b/classpath/java/util/HashSet.java @@ -15,7 +15,7 @@ public class HashSet implements Set { private final HashMap map; - public HashSet(Collection c) { + public HashSet(Collection c) { map = new HashMap(c.size()); addAll(c); } diff --git a/classpath/java/util/LinkedList.java b/classpath/java/util/LinkedList.java index aefa979313..a9f4c442ce 100644 --- a/classpath/java/util/LinkedList.java +++ b/classpath/java/util/LinkedList.java @@ -15,7 +15,7 @@ public class LinkedList implements List { private Cell rear; private int size; - public LinkedList(Collection c) { + public LinkedList(Collection c) { addAll(c); } diff --git a/classpath/java/util/List.java b/classpath/java/util/List.java index 9c997daffa..0577e4b3e7 100644 --- a/classpath/java/util/List.java +++ b/classpath/java/util/List.java @@ -22,6 +22,4 @@ public interface List extends Collection { public void add(int index, T element); public boolean isEmpty(); - - public S[] toArray(S[] a); } diff --git a/classpath/java/util/Vector.java b/classpath/java/util/Vector.java index 54d2b9ccc7..20d6a8323e 100644 --- a/classpath/java/util/Vector.java +++ b/classpath/java/util/Vector.java @@ -21,11 +21,8 @@ public class Vector implements List { this(0); } - public Vector(List list) { - this(list.size()); - for (T o : list) { - add(o); - } + public Vector(Collection source) { + list = new ArrayList(source); } public synchronized int size() {