I want to run this program once to delete the first "Banana" and run this program the second time to delete the second "Banana". The case is when I now running my code, it will only delete the second "Banana" in the array and stops.
Also I want to print the original array before the delete any elements.I used "System.out.println(" " + array)" but it ends up with showing some garbled code.
public static void main(String args[]){
String[] array = {"Fish", "Banana", "Carrot", "Pinapple", null, "Banana" };
String[] newArr = null;
System.out.println("The string is showing as : " );
System.out.println(array.length);
System.out.println(" " + array);
for(int i = 0; i < array.length; i++){
if(array[i] == "Banana"){
newArr = new String[array.length - 1];
for(int index = 0; index < i; index++){
newArr[index] = array[index];
}
for(int j = i; j < array.length - 1; j++){
newArr[j] = array[j+1];
}
}
}
System.out.println("After deleting a element the string is showing as : " );
for(int i = 0; i < newArr.length; i++){
System.out.print(newArr[i] + " ");
}
}
}