From 4d3fd38d54c8e976b73341bef341a11fa1843751 Mon Sep 17 00:00:00 2001 From: Eric Scharff Date: Wed, 26 Sep 2007 10:32:39 -0600 Subject: [PATCH] List now implements various forms of add() --- classpath/java/util/ArrayList.java | 7 +++++++ classpath/java/util/Arrays.java | 4 ++++ classpath/java/util/LinkedList.java | 10 ++++++++++ classpath/java/util/List.java | 4 ++++ classpath/java/util/Vector.java | 4 ++++ 5 files changed, 29 insertions(+) diff --git a/classpath/java/util/ArrayList.java b/classpath/java/util/ArrayList.java index 21d2b093c3..fa0813805c 100644 --- a/classpath/java/util/ArrayList.java +++ b/classpath/java/util/ArrayList.java @@ -67,6 +67,13 @@ public class ArrayList implements List { return false; } + public void add(int index, T element) { + size = Math.max(size+1, index+1); + grow(); + System.arraycopy(array, index, array, index+1, size-index); + array[index] = element; + } + public boolean add(T element) { ++ size; grow(); diff --git a/classpath/java/util/Arrays.java b/classpath/java/util/Arrays.java index 9e6a68eed7..204c18548e 100644 --- a/classpath/java/util/Arrays.java +++ b/classpath/java/util/Arrays.java @@ -25,6 +25,10 @@ public class Arrays { throw new UnsupportedOperationException(); } + public void add(int index, T element) { + throw new UnsupportedOperationException(); + } + public boolean contains(T element) { for (int i = 0; i < array.length; ++i) { if (equal(element, array[i])) { diff --git a/classpath/java/util/LinkedList.java b/classpath/java/util/LinkedList.java index f0980fb982..bdb9b7d589 100644 --- a/classpath/java/util/LinkedList.java +++ b/classpath/java/util/LinkedList.java @@ -107,6 +107,16 @@ public class LinkedList implements List { return true; } + public void add(int index, T element) { + if (index == 0) { + addFirst(element); + } else { + Cell cell = find(index); + Cell newCell = new Cell(element, cell.prev, cell); + cell.prev.next = newCell; + } + } + public void addFirst(T element) { addFirst(new Cell(element, null, null)); } diff --git a/classpath/java/util/List.java b/classpath/java/util/List.java index 6b0dce20f1..0c679415a8 100644 --- a/classpath/java/util/List.java +++ b/classpath/java/util/List.java @@ -5,6 +5,10 @@ public interface List extends Collection { public T remove(int index); + public boolean add(T element); + + 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 c1e465f175..cf2d30f58b 100644 --- a/classpath/java/util/Vector.java +++ b/classpath/java/util/Vector.java @@ -26,6 +26,10 @@ public class Vector implements List { return list.contains(element); } + public synchronized void add(int index, T element) { + list.add(index, element); + } + public synchronized boolean add(T element) { return list.add(element); }