1

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!

2
  • 1
    You're incrementing counter on each step through the first for loop, is that really what you mean? Commented Dec 9, 2013 at 14:21
  • Is there any good reason why you dont use the ListArray? Commented Dec 9, 2013 at 14:22

6 Answers 6

2

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 + " ");
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

It got rid of the error but i still got int x in my array int [] array3 = {0,1,3,2,3,0,3,1}; int x = 3; removeTwo(x,array3); for (int value : array3) System.out.printf("%d",value); Do I use that in my main method?
Use what in your main method? Why would you print the values of array3, which seems to be the original array?
wait nevermind, I wasn't thinking straight. hahaha I need sleep, thanks for your help!
Sure thing, it happens!
1

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.

Comments

0

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.

Comments

0

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).

4 Comments

Is there any method removeAll(int) at List?
int is a primitive object, you can't put that in a List
@TimB that's why he is using Integer
@Stone I know, I was telling Masud :)
0

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

2 Comments

This code would not work, you never increment counter and you also misspelled length twice.
you are right sorry. after array4[counter] = array3[i]; you have to increment the counter counter += 1
0

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();
      }
  }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.