Hi,
this fixes a slight oversight in the add methods and implements
indexOf(E, int) and lastIndexOf(E, int).
Please comment/commit.
Thanks,
Carsten
2006-07-19 Carsten Neumann <[EMAIL PROTECTED]>
* CopyOnWriteArrayList.java (indexOf(E, int)): New method.
(lastIndexOf(E, int)): Likewise.
(add(E)): Increase the size of newData array by one.
(add(int, E)): Likewise.
Index: java/util/concurrent/CopyOnWriteArrayList.java
===================================================================
RCS file: /sources/classpath/classpath/java/util/concurrent/Attic/CopyOnWriteArrayList.java,v
retrieving revision 1.1.2.1
diff -u -r1.1.2.1 CopyOnWriteArrayList.java
--- java/util/concurrent/CopyOnWriteArrayList.java 16 Jun 2006 17:39:13 -0000 1.1.2.1
+++ java/util/concurrent/CopyOnWriteArrayList.java 19 Jul 2006 20:37:41 -0000
@@ -133,6 +133,25 @@
}
/**
+ * Return the lowest index greater equal <code>index</code> at which
+ * <code>e</code> appears in this List, or -1 if it does not
+ * appear.
+ *
+ * @param e the element whose inclusion in the list is being tested
+ * @param index the index at which the search begins
+ * @return the index where <code>e</code> was found
+ */
+ public int indexOf(E e, int index)
+ {
+ E[] data = this.data;
+
+ for (int i = index; i < data.length; i++)
+ if (equals(e, data[i]))
+ return i;
+ return -1;
+ }
+
+ /**
* Returns the highest index at which element appears in this List, or -1 if
* it does not appear.
*
@@ -150,6 +169,25 @@
}
/**
+ * Returns the highest index lesser equal <code>index</code> at
+ * which <code>e</code> appears in this List, or -1 if it does not
+ * appear.
+ *
+ * @param e the element whose inclusion in the list is being tested
+ * @param index the index at which the search begins
+ * @return the index where <code>e</code> was found
+ */
+ public int lastIndexOf(E e, int index)
+ {
+ E[] data = this.data;
+
+ for (int i = index; i >= 0; i--)
+ if (equals(e, data[i]))
+ return i;
+ return -1;
+ }
+
+ /**
* Creates a shallow copy of this ArrayList (elements are not cloned).
*
* @return the cloned object
@@ -255,7 +293,7 @@
public synchronized boolean add(E e)
{
E[] data = this.data;
- E[] newData = (E[]) new Object[data.length];
+ E[] newData = (E[]) new Object[data.length + 1];
System.arraycopy(data, 0, newData, 0, data.length);
newData[data.length] = e;
this.data = newData;
@@ -277,7 +315,7 @@
public synchronized void add(int index, E e)
{
E[] data = this.data;
- E[] newData = (E[]) new Object[data.length];
+ E[] newData = (E[]) new Object[data.length + 1];
System.arraycopy(data, 0, newData, 0, index);
newData[index] = e;
System.arraycopy(data, index, newData, index + 1, data.length - index);