I am having problem with problems with methods indexOf and contains error: no re
ID: 3543853 • Letter: I
Question
I am having problem with problems with methods indexOf and contains error: no returns statement and also indexOf methos has to have an exception. Also trimToSize method is not working well when I am testing it
Any suggestions thanks
import java.lang.IndexOutOfBoundsException;
import java.util.NoSuchElementException;
public class BasicArrayList implements BasicList
{
public static final int DEFAULT_CAPACITY = 100;
private int size;
private Object [] list;
public BasicArrayList()
{
list = new Object[DEFAULT_CAPACITY];
size = 0;
}
public BasicArrayList(int capacity)
{
list = new Object[capacity];
size = 0;
}
public void add(Object element)
{
list[size] = element;
size ++;
}
public void add(int index, Object element)
{
if (index >= size || index < 0)
{
throw new IndexOutOfBoundsException("Out of boundaries");
}
list[index] = element;
size ++;
}
public Object get(int index)
{
if (index >= size || index < 0)
{
throw new IndexOutOfBoundsException("Out of boundaries");
}
return list[index];
}
public Object set(int index, Object element)
{
if (index >= size || index < 0)
{
throw new IndexOutOfBoundsException("Out of boundaries");
}
Object temp = list[index];
list [index] = element;
return temp;
}
public Object remove (int index)
{
if (index >= size || index < 0)
{
throw new IndexOutOfBoundsException("Out of boundaries");
}
Object temp;
temp = list[index];
list[index] = null;
return temp;
}
public int size()
{
return size;
}
public boolean contains(Object element)
{
boolean isEsVerdadero = false;
if(element.equals(null))
{
for(int i = 0; i < list.length; i++)
{
if(list[i].equals(element))
{
isEsVerdadero = true;
}
}
}
else if (size >= 0)
{
for(int i = 0; i < list.length; i++)
{
if(element.equals(list[size]))
{
isEsVerdadero = true;
}
}
}
else
{
isEsVerdadero = false;
}
return isEsVerdadero;
}
public void clear()
{
for (int i = 0; i < size; i ++)
{
list[i] = null;
}
size = 0;
}
public int indexOf(Object element)
{
if(element == null)
{
for(int i = 0; i < list.length; i++)
{
if(list[i] == null)
{
return i;
}
}
}
else
{
for(int i = 0; i < list.length; i++)
{
if(element.equals(list[size]))
{
return i;
}
}
}
return -1;
//throw new NoSuchElementException("Out!");
}
public void trimToSize()
{
int newCapacity = list.length;
Object[] temp;
if(size < newCapacity)
{
temp = list;
temp = new Object [size];
for (int i = 0; i < size; i++)
{
temp[i] = list[i];
}
}
else
{
temp = list;
temp = new Object [newCapacity];
for (int i = 0; i < newCapacity; i++)
{
temp[i] = list[i];
}
}
}
public int capacity()
{
int capacity = list.length;
return capacity;
}
public Object[] unusualMethodForTestDriverOnly()
{
return list;
}
}
Explanation / Answer
public int indexOf(Object element)
{
if(element == null)
{
for(int i = 0; i < list.length; i++)
{
if(list[i] == null)
{
return i;
}
}
}
else
{
for(int i = 0; i < list.length; i++)
{
//if(element.equals(list[size])) This is wrong! we should compare with
//list[i] and not list[size] to get the index of an element
if(element.equals(list[i]))
{
return i;
}
}
}
//return -1 This is not needed. If element is not found then just throw an exception
throw new NoSuchElementException("Out!");
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.