java - Function delete all in arrays class -


how can delete element equal value entered tried code

public void deletall(int v) {     if (i = 0; < count; i++)     {         if (a[i] == v)             (k = i; k < count; k++)                 a[k] = a[k + 1];     } }   

you can't delete elements array.

you can set value 0 or whatever... , shift later elements - can't make array's length shorter was.

so change existing code shift remaining elements and set last element 0, need is:

public void deleteall(int v) {     if (i = 0; < a.length; i++) {         if (a[i] == v) {             // shift elements. note end condition of loop...             (k = i; k < a.length - 1; k++) {                 a[k] = a[k + 1];             }             // set last element 0             a[a.length - 1] = 0;         }     } } 

that's removed use of count, because array has length. if count meant "the number of meaningful elements in array" want change code like:

public void deleteall(int v) {     if (i = 0; < count; i++) {         if (a[i] == v) {             // shift elements. note end condition of loop...             (k = i; k < count; k++) {                 a[k] = a[k + 1];             }             // remember we've got fewer elements             count--;             // set last element 0 (the 1 we've copied             // final "useful" position)             a[count] = 0;         }     } } 

if find wanting this, should use list instead, e.g. arraylist.


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -