Machine-translated by Google. Questions are authored in English.
Top

Discussion

Which one of the following statements about the difference between List.remove(int) and List.remove(Object) is true?

  • A.remove(int) deletes by position while remove(Object) deletes the first equal element.
  • B.Both delete by position; the Object form converts its argument to an index.
  • C.Both delete the first equal element; the int form is simply a convenience overload.
  • D.remove(int) deletes by position and remove(Object) deletes every equal element.

Answer: A

They are overloads with quite different meanings, and on a List<Integer> the choice depends on the static type of the argument: remove(1) deletes index 1, while remove(Integer.valueOf(1)) deletes the element equal to 1. This is the classic reason to reach for an explicit cast or valueOf.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.