i am working on a assignment that has to do with array lists, it mainly has to do with adding new elements, telling then where it is it located, if the memory block is empty , ect. so far i have been having problems with my indexOf method which should display the array cell number that a input element E is in, and if it is not in there it should display a -1.
public class MyArrayList<E> { private E[] data_store = (E[])new Object[2]; private int sizeofa = 0; private void resize()// makes the array list bigger if need { E[] bigspacemaker = (E[])new Object[data_store.length * 2]; for(int x = 0 ; x< sizeofa ; x++) { bigspacemaker[x]= data_store[x]; } data_store = bigspacemaker ; } public int indexOf(E element) // should returns the index of the given element. Returns -1 if element was not found. { boolean pass = false; ; int o = 0 ; while (element!= data_store[o]) { o++; if (element.equals(data_store[o]))//Eclipse says that the problem is here { pass = true; } } if (pass == false) { int z = -1; return z; } else { return o; } } public void add(E element)// adds elements { if (sizeofa == data_store.length) { resize(); } data_store[sizeofa] = element; sizeofa++; } public boolean contains(E element)//checks to see if the array list contains a spesific element { boolean a = false; int count = 0 ; while (count < sizeofa ) { if ( data_store[count] == element ) { a = true; } count++; } return a; } public boolean isEmpty()// checks if array cell is empty { if (data_store[0]==null) { return true; } else { return false; } } public E get(int index) { return data_store[index]; } public int size() { return sizeofa; } }
and here is the code that tests it out
please help me i am really confused on why it dosen't workpublic class MyArrayListDemo1 { public static void main(String[] args) { MyArrayList<String> t = new MyArrayList<String>(); System.out.println("isEmpty? " + t.isEmpty()); t.add("Santa Maria"); t.add("Los Angeles"); t.add("Ventura"); t.add("Thousand Oaks"); System.out.println("isEmpty? " + t.isEmpty()); System.out.println("Contains Santa Maria? " + t.contains("Santa Maria")); System.out.println("Contains Los Angeles? " + t.contains("Los Angeles")); System.out.println("Contains Junk data 75? " + t.contains("Junk data 75")); System.out.println("Contains Ventura? " + t.contains("Ventura")); System.out.println("Contains Thousand Oaks? " + t.contains("Thousand Oaks")); System.out.println("Contains Simi Valley? " + t.contains("Simi Valley")); System.out.println("Contains Westlake? " + t.contains("Westlake")); for (int x = 0; x < 500; x++) { t.add("Junk data " + x); } System.out.println("isEmpty? " + t.isEmpty()); System.out.println("indexOf Ventura: " + t.indexOf("Ventura")); System.out.println("indexOf Junk data 75: " + t.indexOf("Junk data 75"));// it crashes right here System.out.println("indexOf Santa Maria: " + t.indexOf("Santa Maria")); System.out.println("indexOf Thousand Oaks: " + t.indexOf("Thousand Oaks")); System.out.println("indexOf Junk data 499: " + t.indexOf("Junk data 499")); System.out.println("indexOf Los Angeles: " + t.indexOf("Los Angeles")); System.out.println("indexOf Junk data 123: " + t.indexOf("Junk data 123")); System.out.println("isEmpty? " + t.isEmpty()); System.out.println("--------"); System.out.println("Cool! I didn't crash!"); } }
sorry but i forgot to put in the error message:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 512
at MyArrayList.indexOf(MyArrayList.java:28)
at MyArrayListDemo1.main(MyArrayListDemo1.java:26)