java - Cannot insert item into array after deleting another item -
i can delete items array, when try insert item fails on nullpointer exception. happens after deleting. inserting items works normally. thank in advance.
this determines insert new item array stays in order
public int appropriateposition(comparable ap){ int temp = top; if(temp == -1){ temp = 0; } else { for(int = 0; <= top; i++) { if(ap.compareto(sa[i])>0) { temp = + 1; } } } return temp; }
this uses index found in appropriatepostion
public void insert(comparable a){ int loc = appropriateposition(a); if(full() == true) { int expandedsize = sa.length + incrementamount; comparable[] temparray = new comparable[expandedsize]; for(int i= 0; < sa.length; i++) { temparray[i]= sa[i]; } sa = temparray; } for(int = top; >= loc; i--) { sa[i+1] = sa[i]; } sa[loc] = a; top++; } public void find(comparable value2){ comparable value = value2; if (empty() == true){ system.out.println("the array empty"); arrays.fill(sa, null); } else{ int bsvalue = arrays.binarysearch(sa,value); system.out.println("the index is: " + bsvalue); delete(bsvalue); } } // method deletes given value array public void delete(int bs){ int location = bs; comparable[] temparray = new comparable[sa.length -1]; system.arraycopy(sa, 0, temparray, 0, location); if (sa.length != location){ system.arraycopy(sa, location +1 , temparray, location, sa.length - location - 1); sa = temparray; print(); } }
i found it. thank help. had forgot decrement top after deletion.
public void delete(int bs){ int location = bs; comparable[] temparray = new comparable[sa.length -1]; system.arraycopy(sa, 0, temparray, 0, location); if (sa.length != location){ system.arraycopy(sa, location +1 , temparray, location, sa.length - location - 1); sa = temparray; top--; print(); }
Comments
Post a Comment