1
public static int removeElement(int[] array,int size,Scanner in)throws IOException
{
  System.out.println("Enter the number to remove");
  int removeElement = in.nextInt();
  boolean found = false;
  int index = 0;
  for(int i=0; i<size && !found; ++i)
  {
    if(array[i] == removeElement)
      found = true;
      ++index;
  }
  array.splice(index,1);

I got an error on the array.splice(index,1); that says

Cannot invoke splice(int, int) on the array type int[]

Ram
  • 3,092
  • 10
  • 40
  • 56
Zackary
  • 13
  • 4
  • The `splice()` method doesn't exist for Java arrays. `splice()` is javascript only. – CubeJockey Dec 04 '15 at 19:15
  • [Here is an SO post](http://stackoverflow.com/questions/29052240/java-eqivalent-method-of-splicea-b-in-javascript-method) where a user asks if there is a Java equivalent of `splice(a,b,...)` Maybe it will help. – CubeJockey Dec 04 '15 at 19:16

1 Answers1

0

You can do this way:

  System.out.println("Enter the number to remove");
  int removeElement = in.nextInt();

  List<Integer> list = new ArrayList<Integer>(array.length);

  for (int number : array) {
      if (number != removeElement) {
          list.add(number);
      }
  }

  array = new int[list.size()];

  for (int i = 0; i < list.size(); i++) {
      array[i] = list.get(i);
  }

Note that changes made in array (assign a new array instance, for example) are "visible" only in removeElement method scope. I suggest you to use an ArrayList parameter instead of an array of integers, so you could do list.remove(removeElement).

andrucz
  • 1,971
  • 2
  • 18
  • 30
  • Thanks a lot, this worked perfectly, and yes I know it would be better to use ArrayList but this is for a class and my teacher wanted us to use a normal array. – Zackary Dec 05 '15 at 20:50
  • Nice! You're welcome. Could you please mark my answer as correct? – andrucz Dec 06 '15 at 16:40