正确地从一个List <Integer>中移除一个Integer
这是我遇到的一个不错的陷阱。 考虑一个整数列表:
List<Integer> list = new ArrayList<Integer>(); list.add(5); list.add(6); list.add(7); list.add(1);  任何教育猜测当你执行list.remove(1)会发生什么? 怎么样list.remove(new Integer(1)) ? 这可能会导致一些令人讨厌的错误。 
 在处理整数列表时,区分remove(int index)和remove(Object o) (删除通过引用删除元素remove(Object o)的正确方法是什么? 
这里要考虑的要点是提到的@Nikita之一 – 确切的参数匹配优先于自动装箱。
Java总是调用最适合你的论点的方法。 自动装箱和隐式上传只有在没有方法的情况下才能执行,不需要投入/自动装箱。
List接口指定两个删除方法(请注意参数的命名):
-  remove(Object o)
-  remove(int index)
 这意味着list.remove(1)将删除位置1处的对象,并remove(new Integer(1))从该列表中删除指定元素的第一个匹配项。 
你可以使用铸造
 list.remove((int) n); 
和
 list.remove((Integer) n); 
如果n是一个int或Integer并不重要,该方法将始终调用您所期望的。
 使用(Integer) n或Integer.valueOf(n)比new Integer(n)更有效率,因为前两个可以使用Integercaching,而后者将始终创build一个对象。 
我不知道“正确”的方式,但你提出的方式工作得很好:
 list.remove(int_parameter); 
删除元素在给定的位置和
 list.remove(Integer_parameter); 
从列表中删除给定的对象。
这是因为VM首先尝试查找使用完全相同的参数types声明的方法,然后尝试自动装箱。
  list.remove(4)是list.remove(int index)的精确匹配,因此将被调用。 如果要调用list.remove(Object)请执行以下操作: list.remove((Integer)4) 。 
任何教育猜测当你执行list.remove(1)会发生什么? 怎么样list.remove(新的整数(1))?
 没有必要猜测。 第一种情况将导致List.remove(int)被调用,位置1的元素将被移除。 第二种情况将导致List.remove(Integer)被调用,其值等于Integer(1)的元素将被删除。 在这两种情况下,Java编译器都会select最接近的匹配过载。 
是的,这里存在混淆(和错误)的可能性,但这是一个相当不常见的用例。
 当两个List.remove方法在Java 1.2中定义时,重载不是不明确的。 这个问题只是在Java 1.5中引入了generics和自动装箱。 事后看来,如果其中一个删除方法被赋予不同的名称,那就更好了。 但现在已经太晚了。 
 请注意,即使虚拟机没有做正确的事情,它仍然可以通过使用remove(java.lang.Object)对任意对象进行操作的事实来确保正确的行为: 
 myList.remove(new Object() { @Override public boolean equals(Object other) { int k = ((Integer) other).intValue(); return k == 1; } } 
只是我喜欢跟随#decitrig在接受的答案第一评论build议。
 list.remove(Integer.valueOf(intereger_parameter)); 
这帮助了我。 再次感谢#decitrig的评论。 这可能对某个人有帮助。
那么这就是诀窍。
我们来看两个例子:
 public class ArrayListExample { public static void main(String[] args) { Collection<Integer> collection = new ArrayList<>(); List<Integer> arrayList = new ArrayList<>(); collection.add(1); collection.add(2); collection.add(3); collection.add(null); collection.add(4); collection.add(null); System.out.println("Collection" + collection); arrayList.add(1); arrayList.add(2); arrayList.add(3); arrayList.add(null); arrayList.add(4); arrayList.add(null); System.out.println("ArrayList" + arrayList); collection.remove(3); arrayList.remove(3); System.out.println(""); System.out.println("After Removal of '3' :"); System.out.println("Collection" + collection); System.out.println("ArrayList" + arrayList); collection.remove(null); arrayList.remove(null); System.out.println(""); System.out.println("After Removal of 'null': "); System.out.println("Collection" + collection); System.out.println("ArrayList" + arrayList); } } 
现在让我们来看看输出:
 Collection[1, 2, 3, null, 4, null] ArrayList[1, 2, 3, null, 4, null] After Removal of '3' : Collection[1, 2, null, 4, null] ArrayList[1, 2, 3, 4, null] After Removal of 'null': Collection[1, 2, 4, null] ArrayList[1, 2, 3, 4] 
现在让我们分析一下输出:
- 
当从集合中移除3时,它调用以 Object o为参数的集合的remove()方法。 因此它移除物体3。 但在arrayList对象中,它被索引3覆盖,因此第4个元素被删除。
- 
通过对象删除的相同逻辑,在第二个输出中的两种情况下都将删除null。 
 因此,要删除作为对象的数字3 ,我们将明确需要将3作为object 。 
 这可以通过使用包装类Integer进行转换或包装来完成。 
例如:
 Integer removeIndex = Integer.valueOf("3"); collection.remove(removeIndex); 
 简单的Java代码解释用于显示ArrayList.remove(Object o)和ArrayList.remove(int index) 
CODE:
 ArrayList<Integer> list = new ArrayList<Integer>(); list.add(1); list.add(2); list.add(7); list.add(4); list.add(5); list.add(6); list.add(7); for (int a = 0; a < list.size(); a++) { System.out.print("" + list.get(a)); } System.out.print(" "); //CASE 1: We are removing data object 7. list.remove(new Integer(7)); for (int a = 0; a < list.size(); a++) { System.out.print("" + list.get(a)); } System.out.print(" "); //CASE 2: Again we are removing data object 7. list.remove(new Integer(7)); for (int a = 0; a < list.size(); a++) { System.out.print("" + list.get(a)); } System.out.print(" "); //CASE 3: We are removing data at index 1 list.remove(1); for (int a = 0; a < list.size(); a++) { System.out.print("" + list.get(a)); } 
输出:
 1274567 124567 12456 1456 
解释:
情况1:我们正在用数据7删除第一个出现的Integer对象。
 情况2:然后,我们再次进行与CASE 1的剩余数据相同的事情。 
情况3:我们正在删除索引位置1处的数据。
结论:
 以上面的java代码中使用的ArrayList<Integer>为例: ArrayList.remove(Integer object)将简单地移除ArrayList首先出现的特定Integer对象。 但是, ArrayList.remove(int index)将始终删除给定索引位置处的数组元素。 
更多关于 :
 我们不能声明ArrayList<int>因为它是主数据types。 任何具有基类Object的类只在ArrayList通用数据types中指定。 例如: ArrayList<String> , ArrayList<Integer>等