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       }
136 
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       }
154      
155       return false;
156    }
157

This is what the code is supposed to do:

Contains:
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.

indexOf:
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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.