-3

How do I create a method that satisfies the following conditions?

/* 
 * removes all occurrences (if any) of x in the ArrayList object that the parameter list 
 * is referencing. If there is no occurrence of x then the content of the ArrayList
 * is not altered. 
 */
void removeOccurrences(ArrayList<Integer> list, int x)

NOTE: One may think on an algorithm such as the following:

    for (int i=0; i<list.size(); i++)
        if (list.get(i) == x)
          list.remove(i); 
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
IronBoy
  • 39
  • 9

1 Answers1

1

Try using .equals() instead of ==. == checks whether two references point to the same object. .equals() checks whether two objects represent the same data.

Tripp Kinetics
  • 5,178
  • 2
  • 23
  • 37