Java 如何从列表中删除值<;选择项目>;
如何从Java 如何从列表中删除值<;选择项目>;,java,Java,如何从列表中删除“abcd”的值 公共类示例{ 公共静态void main(字符串[]args){ 列表=新的ArrayList(); 添加(新选择项(“abc”); 列表。添加(新选择项(“abcd”); 列表。添加(新选择项(“abcdf”); System.out.println(“大小:+list.size()); System.out.println(“列表:+List”); 列表。删除(新选择项(“abcd”); System.out.println(“List:+List.size
列表中删除“abcd”的值
公共类示例{
公共静态void main(字符串[]args){
列表=新的ArrayList();
添加(新选择项(“abc”);
列表。添加(新选择项(“abcd”);
列表。添加(新选择项(“abcdf”);
System.out.println(“大小:+list.size());
System.out.println(“列表:+List”);
列表。删除(新选择项(“abcd”);
System.out.println(“List:+List.size());
}
}
试试这个:
list.remove(1);
其中1
是索引。这将删除此列表中指定位置的元素
如果要根据元素的状态删除该元素,如下所示:
list.remove(new SelectItem("abcd"));
您必须重写SelectItem
类的和方法,因为:
内部使用.equals()
来比较列表中是否存在元素,如果存在,则删除第一个出现的新选择项(“abcd”)
尝试以下操作:
list.remove(1);
其中1
是索引。这将删除此列表中指定位置的元素
如果要根据元素的状态删除该元素,如下所示:
list.remove(new SelectItem("abcd"));
您必须重写SelectItem
类的和方法,因为:
内部使用.equals()
来比较列表中是否存在元素,如果存在,则删除第一次出现的新选择项(“abcd”)
,以便编写的代码正常工作您需要实现选择项的equals()
方法
从remove
方法的java文档中:
removes the element with the lowest index i such
that (o==null ? get(i)==null : o.equals(get(i)))
请注意所提供条件中的部分o.equals(get(i))
换句话说,ArrayList
的实现搜索要删除的特定对象。它如何判断某个项目是否是请求的对象?当然,它使用equals()
方法,如果该项与您提供的要删除的项相等,则只会删除该项
请注意:为了与其他收集操作保持一致,您还需要实现hashCode()
方法,使其与equals()
方法一致。为了使您编写的代码正常工作,您需要实现equals()
选择项目的方法
从remove
方法的java文档中:
removes the element with the lowest index i such
that (o==null ? get(i)==null : o.equals(get(i)))
请注意所提供条件中的部分o.equals(get(i))
换句话说,ArrayList
的实现搜索要删除的特定对象。它如何判断某个项目是否是请求的对象?当然,它使用equals()
方法,如果该项与您提供的要删除的项相等,则只会删除该项
请注意:为了与其他收集操作保持一致,您还需要实现hashCode()
方法,使其与equals()
方法一致。使用以下方法:
SelectItem selectItem = new SelectItem();
selectItem.setValue("abcd");
list.remove(selectItem); // Just call the remove method
// If present, it'll remove it, else, won't do anything
使用以下命令:
SelectItem selectItem = new SelectItem();
selectItem.setValue("abcd");
list.remove(selectItem); // Just call the remove method
// If present, it'll remove it, else, won't do anything
在JSF上下文中,SelectItem是UISelectOne中的有效选项,当您使用只接受参数的构造函数时,参数将设置为它的值
请尝试以下代码:
public SelectItem getItem(List<SelectItem> items, Object value) {
for (SelectItem si : items) {
if (si.getValue().equals(si)) {
return si;
}
}
return null;
}
这不会在迭代时修改列表,它会返回所需的元素,在您可以删除它或根据需要使用它之后
Cheers在JSF的上下文中,SelectItem是UISelectOne中的有效选项,当您使用只接受参数的构造函数时,参数将设置为它的值
请尝试以下代码:
public SelectItem getItem(List<SelectItem> items, Object value) {
for (SelectItem si : items) {
if (si.getValue().equals(si)) {
return si;
}
}
return null;
}
这不会在迭代时修改列表,它会返回所需的元素,在您可以删除它或根据需要使用它之后
干杯看来。我能看到的唯一选择是迭代每个元素并确定索引,然后使用。似乎是这样。我能看到的唯一选项是迭代每个元素并确定索引,然后使用。您需要使用迭代器来遍历列表。无论何时找到匹配项(我认为可以使用SelectItem的getValue()
方法),都可以使用迭代器将其删除
由于无法更改SelectItem
的equals()
方法,因此请像这样使用迭代器
Iterator<SelectItem> itr = list.iterator();
while (itr.hasNext()) {
SelectItem si = itr.next();
if (si.getValue().equals("abcd")) {
itr.remove();
// You removed what you wanted, you can break here, if you want
}
}
Iterator itr=list.Iterator();
while(itr.hasNext()){
选择item si=itr.next();
如果(si.getValue().equals(“abcd”)){
itr.remove();
//你拿走了你想要的东西,你可以在这里打破,如果你想的话
}
}
您需要使用迭代器来遍历列表。无论何时找到匹配项(我认为可以使用SelectItem的getValue()
方法),都可以使用迭代器将其删除
由于无法更改SelectItem
的equals()
方法,因此请像这样使用迭代器
Iterator<SelectItem> itr = list.iterator();
while (itr.hasNext()) {
SelectItem si = itr.next();
if (si.getValue().equals("abcd")) {
itr.remove();
// You removed what you wanted, you can break here, if you want
}
}
Iterator itr=list.Iterator();
while(itr.hasNext()){
选择item si=itr.next();
如果(si.getValue().equals(“abcd”)){
itr.remove();
//你拿走了你想要的东西,你可以在这里打破,如果你想的话
}
}
SelectItem似乎没有正确实现equals&hashCode方法。在这种情况下,您可以迭代列表并删除相应的项,或者保留对实际选择项的引用并直接删除该引用。看来SelectItem没有正确实现equals&hashCode方法。在这种情况下,您可以迭代列表并删除相应的项,也可以保留对实际选择项的引用并直接删除该引用。尝试以下代码
public SelectItem getItemToRemove(List<SelectItem> items, SelectItem item) {
for (SelectItem si : items) {
if (si.getValue().equals(item.getValue())) {
return si;
}
}
return null;
}
public-SelectItem-getItemToRemove(列出项目,选择项目){
对于(选择项目si:items){
如果(si.getValue().equals(item.getValue())){