-3

Code:

String[] myStringArray = {"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};

String input = "Monday"

Output:

String[] newInput = {"Tuesday","Wednesday","Thursday","Friday","Saturday"};

So how to remove the specific input to update in array in java?

bn4t
  • 1,448
  • 1
  • 11
  • 23
Star
  • 735
  • 3
  • 13
  • 34
  • Arrays are fixed size. So unless you want to replace the elements that match your input with "null" you need to create a new array where you only add those elements that don't match the input. – OH GOD SPIDERS Jan 11 '18 at 15:29

2 Answers2

1

Just try this:

public static void main(final String[] args) {
    String[] myStringArray = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
    String input = "Monday";
    String[] output = Arrays.stream(myStringArray).filter(e -> !e.equals(input)).toArray(String[]::new);
    System.out.println(Arrays.toString(output));
}
0
String[] myStringArray = {"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
String input = "Monday";
String[] newInput = new String[5];
int j = 0;
for(int i=0; i< myStringArray.length; i++)
{
  if(!myStringArray[i].equals(input))
     {
       newInput[j] = myStringArray[i];
       j++;
     }
}

The really horrible piece of code, but I hope it helps - I kept it as simple as possible.