-1

I try to remove an element from java array and shift the array, update the total value and the count. Here is the code segment that I wrote, it gives me wrong result, how can I fix it?

public void remove(int index)
{
    if(index > count-1 || index <0)
        System.out.println("Incorrect index.");
    else{
        if(index ==1){
            total -= numbers[index];
            numbers = new int [0];
            count--;
        }else{
            total -= numbers[index];
            for(int i=index; i < count; i++)
                numbers[i] = numbers[i+1];
            count--;
        }

    } 
}

I test the code with this;

NumberList n10 = new NumberList(-5); 
n10.add(45); 
n10.remove(0);
System.out.println(n10); 
n10.add(12); 
n10.add(25); 
n10.add(20);
System.out.println(n10); 
n10.remove(1);
n10.remove(1);
n10.remove(0);
System.out.println(n10);

I get

Size of the array must be greater than 0. [Empty List] [12, 25, 20]
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
    at NumberList.remove(NumberList.java:113)   
        at Prelab8.main(Prelab8.java:102)
Duncan Jones
  • 67,400
  • 29
  • 193
  • 254
user3646090
  • 35
  • 1
  • 4
  • possible duplicate of [Delete item from array and shrink array](http://stackoverflow.com/questions/4870188/delete-item-from-array-and-shrink-array) – Illidanek May 27 '14 at 09:09
  • 1
    Do you mean `count == 1` in the `if`? – cyon May 27 '14 at 09:09
  • 1
    Please explain what you expected to happen and what actually happened. Include the stack trace for any errors. Ideally, extend your code example to be a fully working example that demonstrates your problem. – Duncan Jones May 27 '14 at 09:11
  • Yes, I update it as count,but I still get errors. – user3646090 May 27 '14 at 09:12

1 Answers1

1

You will get arrayindexoutofbound in the for loop. It should be

   for(int i=index; i < count-1; i++)

Final code sample:

public class Test {
static int count;
static int total;
static int [] numbers = {1,2,3,4};
    public static void main(String[] args) throws IOException {


        count = numbers.length;
        for (int i : numbers) {
            total = total+i;
        }
        remove(3);

        System.out.println(total);
        System.out.println(count);
    }
    public static void remove(int index)
    {
        if(index > count-1 || index <0)
            System.out.println("Incorrect index.");
        else{
            if(index ==1){
                total -= numbers[index];
                numbers = new int [0];
                count--;
            }else{
                total -= numbers[index];
                for(int i=index; i < count-1; i++)
                    numbers[i] = numbers[i+1];
                count--;
            }

        } 
    }
}
Hirak
  • 3,601
  • 1
  • 22
  • 33
  • I check your code but it still gives java.lang.ArrayIndexOutOfBoundsException: 1 at NumberList.remove(NumberList.java:104) at Prelab8.main(Prelab8.java:102) – user3646090 May 27 '14 at 09:18
  • I dont have your NumberList.java or Prelab8.main classes so cannot comment on the error – Hirak May 27 '14 at 09:21