如何从列表中删除整数? [英] How to remove integer from list?

查看:37
本文介绍了如何从列表中删除整数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从整数数组列表中删除整数.我对字符串和其他对象没有问题.但是当我删除时,整数被视为索引而不是对象.

I need to remove integer from integer arraylist. I have no problem with strings and other objects. But when i removing, integer is treated as an index instead of object.

List<Integer> list = new ArrayList<Integer>();
list.add(1);
list.add(2);
list.add(300);
list.remove(300);

当我尝试删除 300 时,我得到:06-11 06:05:48.576: E/AndroidRuntime(856): java.lang.IndexOutOfBoundsException: Invalid index 300, size is 3

When I am trying to remove 300 i am getting: 06-11 06:05:48.576: E/AndroidRuntime(856): java.lang.IndexOutOfBoundsException: Invalid index 300, size is 3

推荐答案

这是正常的,列表的 .remove() 方法有两种版本:一种以整数作为参数并删除此索引处的条目,另一个将泛型类型作为参数(在运行时为 Object)并将其从列表中删除.

This is normal, there are two versions of the .remove() method for lists: one which takes an integer as an argument and removes the entry at this index, the other which takes a generic type as an argument (which, at runtime, is an Object) and removes it from the list.

而且方法的查找机制总是首先选择更具体的方法...

And the lookup mechanism for methods always picks the more specific method first...

您需要:

list.remove(Integer.valueOf(300));

为了调用正确版本的.remove().

这篇关于如何从列表中删除整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆