简体   繁体   中英

Removing specific value from array (java)

i have integer a = 4 and array b 7,8,9,4,3,4,4,2,1

i have to write a method that removes int ALL a from array b

desired result 7,8,9,3,2,1

This is what I have so far,

    public static int[] removeTwo (int x, int[] array3)
{
    int counter = 0;
    boolean[] barray = new boolean [array3.length];
    for (int k=0; k<array3.length; k++)
    {
        barray[k] = (x == array3[k]);
        counter++;
    }

     int[] array4 = new int [array3.length - counter];
     int num = 0;
     for (int j=0; j<array3.length; j++)
{
     if(barray[j] == false)
    {
        array4[num] = array3[j];
        num++;
    }

}
     return array4;

I get this error

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
    at Utility.removeTwo(Utility.java:50)
    at Utility.main(Utility.java:18)

Java Result: 1

Any help would be much appreciated!

The error stems from this for loop:

for (int k=0; k<array3.length; k++)
{
    barray[k] = (x == array3[k]);
    counter++;
}

when you create int[] array4 = new int [array3.length - counter]; you are creating an array with size 0. You should only increment the counter if the item is the desired item to remove:

for (int k=0; k<array3.length; k++)
{
    boolean b = (x == array3[k]);
    barray[k] = b;
    if(b) {
        counter++;
    }
}

To answer your question in the comment, the method should be called and can be checked like this:

public static void main(String[] args) {
    int[] array3 = {0,1,3,2,3,0,3,1};
    int x = 3;
    int[] result = removeTwo(x, array3);
    for (int n : result) {
        System.out.print(""+ n + " ");
    }
}

On this line:

 int[] array4 = new int [array3.length - counter];

You create an array with size 0, as counter is equal to array3.length at this point.

This means that you cannot access any index in that array.

You are creating

int[] array4 = new int [array3.length - counter];// 0 length array.

you can't have 0th index there. At least length should 1 to have 0th index.

BTW my suggestion, it is better to use List . Then you can do this easy.

Really an Array is the wrong tool for the job, since quite apart from anything else you will end up with stray values at the end that you cannot remove. Just use an ArrayList and that provides a removeAll() method to do what you need. If you really need arrays you can even do:

List<Integer> list = new ArrayList(Arrays.asList(array))
list.removeAll(4);
array = list.toArray();

(Exact method names/parameters may need tweaking as that is all from memory).

the simplest way is to work with a second array where you put in the correct values

something likte that

public static int[] removeTwo (int x, int[] array3)
{
    int counter = 0;
    int[] array4 = new int[array3.lenght];
    for (int i = 0; i < array3.lenght; i ++) {
       if(array3[i] == x){
           array4[counter] = array3[i];
       }
    }
    return array4;
}

anoterh way is to remove the x calue from the array3 and shift the values behind forward

The best way to remove element from array is to use List with Iterator . Try,

 Integer[] array = {7, 8, 9, 4, 3, 4, 4, 2, 1};
 List<Integer> list = new ArrayList(Arrays.asList(array));
 for(Iterator<Integer> it=list.iterator();it.hasNext();){
     if(it.next()==4){
        it.remove();
      }
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM