Properly removing an Integer from a List

前端 未结 8 1674
面向向阳花
面向向阳花 2020-11-22 03:57

Here\'s a nice pitfall I just encountered. Consider a list of integers:

List list = new ArrayList();
list.add(5);
list.add(6);         


        
8条回答
  •  情歌与酒
    2020-11-22 04:55

    I don't know about 'proper' way, but the way you suggested works just fine:

    list.remove(int_parameter);
    

    removes element at given position and

    list.remove(Integer_parameter);
    

    removes given object from the list.

    It's because VM at first attempts to find method declared with exactly the same parameter type and only then tries autoboxing.

提交回复
热议问题