Ive written these two methods but they are giving me lots of problems.

public int indexOf(Object element)
128    {
129       for(int i = 0; i < list.length; i++)
130       {
131          if(list[i] != null && list[i].equals(element))
132          {
133             return i;
134          }
135       }
137       throw new IndexOutOfBoundsException();
138    }
public boolean contains(Object element)
146    {
147       for(int i = 0; i < list.length; i++)
148       {
149          if(list[i] != null && list[i].equals(element))
150          {
151             return true;
152          }
153       }
155       return false;
156    }

This is what the code is supposed to do:

Using the provided element's equals method, this method determines if the list contains the specified element or not. True if the list contains an element logically equal to the specified element, otherwise false.

Using the provided element's equals method, this method returns the index of the first element in the list that is equal to the provided element, if any. returns The index of the first matching element in the list.

Any help to fix these methods or rewrite them would be of great help. Ive been coding an extreme amount in the past 24 hours and im down to these two.

5 Years
Discussion Span
Last Post by Taywin

I'm still not sure about "equals()" method for Object class. Does it really work the way you want? By the way, what are problems you are getting now? Error? Exception?

By the way, to do contains, you could simply call like this...

public boolean contains(Object element) {
  return (indexOf(element)>=0);

If you get an index value, you found it, right? If you don't (-1), you won't find it. It is simple to call another method which has very similar implementation. Then what you need to worry about is the indexOf() method.

Edited by Taywin: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.