从通用列表Java中删除项目? [英] Removing item from generic list java?

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

问题描述

我需要从Java的通用列表中删除一个项目,但是我不知道该怎么做.如果它是一个int列表,则将其设置为零,如果它是字符串,则将其设置为null.我该如何使用通用列表做到这一点,而不能使用Arraylist或类似方法,我必须自己编写该方法.

I need to remove an item from a generic list in java, but I don't know how to do this. If it was a list of int, I would just set it to zero, if it was strings I would set it to null. How can I do this with a generic list, and I can't use an methods of Arraylist or anything like that, I have to write the method myself.

推荐答案

您可以使用 Iterator.remove() 在迭代List时.因此,例如,要删除List中的所有项目,您可以

You can remove an individual object instance with List.remove(Object) or you can remove a specific instance from a specific index with List.remove(int). You can also call Iterator.remove() while you iterate the List. So, for example, to remove every item from a List you could do

Iterator<?> iter = list.iterator();
while (iter.hasNext()) {
  iter.remove();
}

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

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