Hello,
I am writing a class (pretty much an ADT) for an array implementation. I wrote the code and it both compiles and works, but in the process, I have encountered a problem. This problem is that the add method in the class does not work properly (neither do the remove methods). Although the logic appears to be correct and it compiles correctly, the classes do not serve the function I was hoping. I was hoping that I could just add an element and the class would dynamically add it (make more space or move items as needed). The remove methods don't seem to serve their function either. Would anybody be able to help me troubleshoot and sort this problem out? I really appreciate it and thank you in advance!
P.S. If you see any other problems with the code, please feel free to let me know! Thanks again!
Below is the code for the array implementation class:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package arrayimplementation2; /** * * @author Ben */ public class arrayimplementation { protected int length; protected int size; public double list[]; public arrayimplementation () { size = 10; length = 0; list = new double [size]; } public arrayimplementation (int arraysize) { if(size <= 0) System.err.println("The array size mist be positive and greater than zero!"); else{ arraysize = size; length = 0; list = new double [arraysize]; } } public boolean isEmpty() { return (length == 0); } public boolean isFull () { return (length == size); } public int noOfelements() { return length; } public int listcapacity () { return size; } public void output() { for(int x=0; x< size; x++){ System.out.print("{" + list[x]+"}"); } } public boolean isEqual(int location, int location2) { if (location <0 || location >= length){ System.err.println("The location of the item to be compared is out of range!"); return false; } return (list[location]== list[location2]); } public void Remove(int location) { if(location < 0 || location >= length) System.err.println("The location of the item to be removed is out of range!"); else{ /*for(int i=0; i< length -1; i++) list[i-1] = i; length --;*/ //double removedelement = list[location]; for(int i = location + 1; i < size; i++) list[i-1] = list[i]; //return removedelement; } } public void Remove (double item) { int location; if(length == 0) System.err.println("Cannot delete an element from an empty list!"); else { location = search(item); if (location != -1) Remove(location); else System.out.println("The item to be deleted is not in the list!"); } } public int getindex( double item) { for(int i=0; i<size; i++){ if(list[i] == item) return i; } return -1; } public void clear(){ list[size]=0; length = 0; System.gc(); } public void add(int location, double item) { if(location<0 || location > size) System.err.println("The positon of the item to be added is out of range!"); else if (length == size){ double[] list2 = new double[length * 2]; System.arraycopy(list2, 0, list2, 0, list2.length); list2 = list; } else if(location == length){ list[location] = item; length++; } else { for( int i = size-1; i > location; i--) list[i] = list[i-1]; list[location] = item; length ++; } } public int search (double item) { int location; boolean found = false; for(location = 0; location < length; location++) if(list[location] == item) { found = true; break; } if (found) return location; else return -1; } }