java - Deleting an item from an array using arraycopy -
i having problems deleting item array using arraycopy. have 2 methods find (which locates index of item deleted) , delete (which deletion). doesn't delete anything. thank in advance.
public void find(comparable value2){ scanner sc = new scanner(system.in); comparable value = value2; if (empty() == true){ system.out.println("the array empty"); } else{ int bsvalue = arrays.binarysearch(sa,value); system.out.println("the index is: " + bsvalue); delete(bsvalue); } } 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); } }
you allocate temparray
, copy data it, , abandon reference. result, original array (sa
) stays was.
presumably meant make sa
point new array:
sa = temparray;
Comments
Post a Comment