从仅给定一个属性的 ArrayList 中删除一个对象
Remove an object from an ArrayList given only one attribute
我有一个项目数组列表,我希望能够通过仅输入一个项目属性从列表中删除一个项目,例如它的编号 (int ItemNumber)。我也想在检查物品数量时做同样的事情。
这些是我的equals()
& contains()
方法,我需要在这里做任何更改吗?
public boolean contains(T anEntry) {
boolean found = false;
for (int index = 0; !found && (index < numberOfEntries); index++) {
if (anEntry.equals(list[index]))
found = true;
}//end for
return found;
} // end contains
public boolean equals(Object object){
Item item = (Item) object;
if (itemNo == item.itemNo)
return true;
return false;
}
您要删除特定索引处的对象吗?我不完全确定 'number field' 是什么意思。如果是这样,跳转到方法:remove(int):
http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#remove%28int%29
编辑:如果你想 find/adjust 数组列表中对象的一个字段,你可以这样做(我自己的一段代码):
public boolean studentHasISBN(ArrayList<Student> st, String s){
for(Student j : st) {
if(s.equals(j.getRentedBookISBN()))
return true;
}
return false;
}
您所要做的就是遍历列表,然后搜索要查找的字段。然后使用 remove(int) 方法。
直接使用ArrayLists
的remove
函数在Java
:
theNameOfYourArrayList.remove(ItemNumber);
删除索引为(int ItemNumber)
的元素
检查项目编号为 (int ItemNumber)
的元素是否存在于您的 ArrayList
中(假设称为 theNameOfYourArrayList
):
theNameOfYourArrayList.get(ItemNumber);
如果您更改 class Item
equals()
和 compareTo()
方法,使它们只检查一个对象字段,例如 quantity
,它可能会导致应用程序其他部分出现奇怪的行为。例如,itemNo
、itemName
、itemPrice
不同但数量相同的两个项目可以认为是相等的。此外,如果不每次都更改 equals()
代码,您将无法更改比较属性。
此外,创建自定义 contains()
方法没有任何意义,因为它属于 ArrayList
class,而不属于 Item
.
如果可以使用Java8,一个干净的方法是使用新的Collection
的removeIf
方法:
假设您有一个具有 num
和 name
属性的 Item
class:
class Item {
final int num;
final String name;
Item(int num, String name) {
this.num = num;
this.name = name;
}
}
给定一个名为 items
的 List<Item>
和一个名为 number
的 int
变量,代表您要删除的项目的编号,您可以简单地执行以下操作:
items.removeIf(item -> item.num == number);
如果您无法使用 Java8,您可以通过使用自定义比较器、二进制搜索和虚拟对象来实现。
您可以为需要查找的每个属性创建自定义比较器。 num
的比较器如下所示:
class ItemNumComparator implements Comparator<Item> {
@Override
public int compare(Item a, Item b) {
return (a.num < b.num) ? -1 : ((a.num == b.num) ? 0 : 1);
}
}
然后您可以使用比较器对列表中的所需元素进行排序和搜索:
public static void main(String[] args) {
List<Item> items = new ArrayList<>();
items.add(new Item(2, "ball"));
items.add(new Item(5, "cow"));
items.add(new Item(3, "gum"));
Comparator<Item> itemNumComparator = new ItemNumComparator();
Collections.sort(items, itemNumComparator);
// Pass a dummy object containing only the relevant attribute to be searched
int index = Collections.binarySearch(items, new Item(5, ""), itemNumComparator);
Item removedItem = null;
// binarySearch will return -1 if it does not find the element.
if (index > -1) {
// This will remove the element, Item(5, "cow") in this case, from the list
removedItem = items.remove(index);
}
System.out.println(removedItem);
}
例如,要搜索名称等其他字段,您需要创建一个名称比较器并使用它进行排序和 运行 列表中的二进制搜索。
请注意,此解决方案有一些缺点。除非您完全确定列表自上次排序后没有更改,否则您必须 在运行使用binarySearch()
方法之前对其重新排序。否则,它可能无法找到正确的元素。排序复杂度为 O(nlogn)
,因此 运行 多次排序可能会变得非常昂贵,具体取决于列表的大小。
我假设 'number field' 你的意思是你用 Integer 数据类型调用了 ArrayList。对于您的问题,我有几种不同的解决方案:
ArrayLists,假设 ArrayList 是 ArrayList<Integer> numList = new ArrayList<Integer>();
,您可以简单地编写一个方法来搜索 'numList' 并删除数字所在的索引。问题是,在 ArrayLists 中包含和查找可能会很慢。
public void deleteNumField(int field) {
// this will stop any error if field isn't actually in numList
// and it will remove the first index of field in the ArrayList
if(numList.contains(field)) numList.remove(numList.find(field));
}
HashSets,HashSets 是一种方便的数据类型,类似于 ArrayList,除了它的数据是它的 'index'(sortof)。我不会深入探讨它们的工作原理,但我会说在其中进行搜索被认为是 O(1)。这将使您的删除变得非常容易和快速。注意:HashSet假设没有重复的数字,如果有使用HashMap。
HashSet<Integer> numList = new HashSet<Integer>();
public void deleteNumField(int field) {
// this will stop errors from attempting to remove a
// non-existant element, and remove it if it exists.
if(numList.contains(field)) numList.remove(field);
}
有关 HashMap、HashSet 和 ArrayList 的更多信息,请参阅:
http://docs.oracle.com/javase/8/docs/api/
我有一个项目数组列表,我希望能够通过仅输入一个项目属性从列表中删除一个项目,例如它的编号 (int ItemNumber)。我也想在检查物品数量时做同样的事情。
这些是我的equals()
& contains()
方法,我需要在这里做任何更改吗?
public boolean contains(T anEntry) {
boolean found = false;
for (int index = 0; !found && (index < numberOfEntries); index++) {
if (anEntry.equals(list[index]))
found = true;
}//end for
return found;
} // end contains
public boolean equals(Object object){
Item item = (Item) object;
if (itemNo == item.itemNo)
return true;
return false;
}
您要删除特定索引处的对象吗?我不完全确定 'number field' 是什么意思。如果是这样,跳转到方法:remove(int):
http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#remove%28int%29
编辑:如果你想 find/adjust 数组列表中对象的一个字段,你可以这样做(我自己的一段代码):
public boolean studentHasISBN(ArrayList<Student> st, String s){
for(Student j : st) {
if(s.equals(j.getRentedBookISBN()))
return true;
}
return false;
}
您所要做的就是遍历列表,然后搜索要查找的字段。然后使用 remove(int) 方法。
直接使用ArrayLists
的remove
函数在Java
:
theNameOfYourArrayList.remove(ItemNumber);
删除索引为(int ItemNumber)
的元素
检查项目编号为 (int ItemNumber)
的元素是否存在于您的 ArrayList
中(假设称为 theNameOfYourArrayList
):
theNameOfYourArrayList.get(ItemNumber);
如果您更改 class Item
equals()
和 compareTo()
方法,使它们只检查一个对象字段,例如 quantity
,它可能会导致应用程序其他部分出现奇怪的行为。例如,itemNo
、itemName
、itemPrice
不同但数量相同的两个项目可以认为是相等的。此外,如果不每次都更改 equals()
代码,您将无法更改比较属性。
此外,创建自定义 contains()
方法没有任何意义,因为它属于 ArrayList
class,而不属于 Item
.
如果可以使用Java8,一个干净的方法是使用新的Collection
的removeIf
方法:
假设您有一个具有 num
和 name
属性的 Item
class:
class Item {
final int num;
final String name;
Item(int num, String name) {
this.num = num;
this.name = name;
}
}
给定一个名为 items
的 List<Item>
和一个名为 number
的 int
变量,代表您要删除的项目的编号,您可以简单地执行以下操作:
items.removeIf(item -> item.num == number);
如果您无法使用 Java8,您可以通过使用自定义比较器、二进制搜索和虚拟对象来实现。
您可以为需要查找的每个属性创建自定义比较器。 num
的比较器如下所示:
class ItemNumComparator implements Comparator<Item> {
@Override
public int compare(Item a, Item b) {
return (a.num < b.num) ? -1 : ((a.num == b.num) ? 0 : 1);
}
}
然后您可以使用比较器对列表中的所需元素进行排序和搜索:
public static void main(String[] args) {
List<Item> items = new ArrayList<>();
items.add(new Item(2, "ball"));
items.add(new Item(5, "cow"));
items.add(new Item(3, "gum"));
Comparator<Item> itemNumComparator = new ItemNumComparator();
Collections.sort(items, itemNumComparator);
// Pass a dummy object containing only the relevant attribute to be searched
int index = Collections.binarySearch(items, new Item(5, ""), itemNumComparator);
Item removedItem = null;
// binarySearch will return -1 if it does not find the element.
if (index > -1) {
// This will remove the element, Item(5, "cow") in this case, from the list
removedItem = items.remove(index);
}
System.out.println(removedItem);
}
例如,要搜索名称等其他字段,您需要创建一个名称比较器并使用它进行排序和 运行 列表中的二进制搜索。
请注意,此解决方案有一些缺点。除非您完全确定列表自上次排序后没有更改,否则您必须 在运行使用binarySearch()
方法之前对其重新排序。否则,它可能无法找到正确的元素。排序复杂度为 O(nlogn)
,因此 运行 多次排序可能会变得非常昂贵,具体取决于列表的大小。
我假设 'number field' 你的意思是你用 Integer 数据类型调用了 ArrayList。对于您的问题,我有几种不同的解决方案:
ArrayLists,假设 ArrayList 是
ArrayList<Integer> numList = new ArrayList<Integer>();
,您可以简单地编写一个方法来搜索 'numList' 并删除数字所在的索引。问题是,在 ArrayLists 中包含和查找可能会很慢。public void deleteNumField(int field) { // this will stop any error if field isn't actually in numList // and it will remove the first index of field in the ArrayList if(numList.contains(field)) numList.remove(numList.find(field)); }
HashSets,HashSets 是一种方便的数据类型,类似于 ArrayList,除了它的数据是它的 'index'(sortof)。我不会深入探讨它们的工作原理,但我会说在其中进行搜索被认为是 O(1)。这将使您的删除变得非常容易和快速。注意:HashSet假设没有重复的数字,如果有使用HashMap。
HashSet<Integer> numList = new HashSet<Integer>(); public void deleteNumField(int field) { // this will stop errors from attempting to remove a // non-existant element, and remove it if it exists. if(numList.contains(field)) numList.remove(field); }
有关 HashMap、HashSet 和 ArrayList 的更多信息,请参阅: http://docs.oracle.com/javase/8/docs/api/