0

Ill try to clarify.

I have:

public static void main(String[] args) {
    int a[] = {5, 6 ,7 ,8 ,9 ,10};
    removeArray takeOff = new removeArray();
    takeOff.remove(3);

    for (int i = 0 ; i < a.length ; i++){
        System.out.print(" "+a[i]);
    }
}

That should print out:

5, 6, 7, 9, 10.

The problem is the remove method, how should I write that. I have:

public class removeArray {

public void remove(int index) {       

    if (index > 0) {
        System.arraycopy(testThatArray.a, 0, testThatArray.a, 0, index);
    }

    if (index < testThatArray.a.length - 1) {
        System.arraycopy(testThatArray.a, index + 1, testThatArray.a, index, testThatArray.a.length - index - 1);
    }

}

}

Still, no?

UPDATE: I got it to print 5 6 7 9 10 10, how can i remove the last value?

Jason Marks
  • 247
  • 1
  • 4
  • 11
  • so what is the problem ? would be great if you could post your code so that we can have a look at it – Satya Oct 23 '12 at 03:04
  • I'm not writing any program, just a general question on how to write a method to remove values from an array by the position, hope that's not too broad... – Jason Marks Oct 23 '12 at 03:08
  • You should use List, otherwise, this is all you need http://stackoverflow.com/questions/457453/remove-element-of-a-regular-array – Quannt Oct 23 '12 at 03:17
  • I don't understand why you should create a new array to remove an item. If you have a class that controls the array behavior (add, get, search, delete operations) then just have an `int size` to control the navigation and when you want to remove an element you can just _move all the items next to the index you want to delete one space to the left_ (it's easier for me explain it in code than in words). – Luiggi Mendoza Oct 23 '12 at 03:36

5 Answers5

2

you can't delete the index or element of array because it's already sized in the memory so you can't use delete but you can put two values { null or 0 } or another values you want to show that you already get of this.

Kawarezmey
  • 21
  • 2
1

Arrays cannot be resized. Use a List<Integer> (such as an ArrayList<Integer>) if you want a collection with resizability.

Otherwise, you'll have to implement this yourself by making a copy of the array that is one element smaller than the original array. Then copy all of the values except the one at the specified index from the old array to the new array.

Matt Ball
  • 354,903
  • 100
  • 647
  • 710
  • Are you sure? i was thinking something like copying everything from the old array (array x) into a new array (array b) up until the index you want to remove then copy everything at index+1 until the correct size of the new array, would that do it? – Jason Marks Oct 23 '12 at 03:14
  • Yes. Is that not equivalent to the steps I described? – Matt Ball Oct 23 '12 at 03:15
  • Sorry I stopped reading when you suggested using `List` my bad... but that is what I wanna know how to do, because I can't put it into code, I think I'm doing it wrong... (Like I know what to do, just not how to write it...) – Jason Marks Oct 23 '12 at 03:17
  • Any particular reason you're not just using a list? – Matt Ball Oct 23 '12 at 03:18
  • 1
    I'll add to this that you might want to add your array in the parameter of the method to remove, then return the new array. Otherwise that array is out of scope. Unless it's global. – robx Oct 23 '12 at 03:24
0

Arrays are fixed length sets. You cant remove a value. If you wan't to add and remove things use a List or ArrayList

ghostbust555
  • 2,040
  • 16
  • 29
0

Create a new array or resize it with the size of the actual array less one and loop through all the values in the array to fill the new array, but skip the one specified in the .remove method.

Keytotruth
  • 31
  • 2
0

ArrayUtils#removeElement will probably do what you want. However, consider using a List instead.

hoipolloi
  • 7,984
  • 2
  • 27
  • 28