Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Help in extending ArrayList class...

807599Nov 24 2006 — edited Nov 24 2006
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.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 22 2006
Added on Nov 24 2006
5 comments
499 views