Hi all,
I am writing one new class which extends the
java.util.ArrayList class. My class is below.
public class MyArrayList<E> extends ArrayList {
private ArrayList<E> list = null;
public MyArrayList () {
list = new ArrayList<E> ();
}
public int indexOf (Object elem, int count) {
int index = 0;
if (count <= 0) {
throw new IllegalArgumentException ("Illegal argument: " + count);
}
while (index != -1) {
index = super.indexOf (elem);
if (index != -1) {
list = (ArrayList) list.subList(index + 1, list.size());
if (count-- == 0) {
break;
}
}
}
return index;
}
}
It has a method
indexOf() which returns the index of the nth occurrence of the element. Please don't care about the logic of the method. I will fix it if anything is wrong. My question is, am i doing this in a clean way or just making ugly??? Also, when i access this method from another class like the following, it says the method is not applicable for the arguments (String, int)
ArrayList<String> list = new MyArrayList<String> ();
System.out.println (list.indexOf("Jegan", 2));
Thanks for ur time.