[英]How do I use an ArrayList as a parameter in a method to remove elements from the class's ArrayList?
My friend and I are studying for our programming exam doing the Sample Programming Exam, and are having trouble using an Array List as a parameter in a method to remove multiple items from an Array List in the same class. 我和我的朋友正在为进行示例编程考试的编程考试而学习,并且在使用数组列表作为从同一类的数组列表中删除多个项目的方法中使用参数时遇到了麻烦。
We have tried searching the web and using our BlueJ textbook to find a solution. 我们尝试在网上搜索并使用我们的BlueJ教科书找到解决方案。
The instruction on our assignment sheet says, "Write a method removeItemFromOrder with an array of strings itemArray parameter to remove multiple food items from the order." 作业单上的说明说:“编写一个方法removeItemFromOrder和一个字符串数组itemArray参数,以从订单中删除多个食品。”
import java.util.ArrayList;
/**
* Write a description of class Order here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Order
{
// instance variables - replace the example below with your own
private ArrayList<OrderedItem> order;
/**
* Constructor for objects of class Order
*/
public Order()
{
order = new ArrayList<>();
}
/**
* Returns the order collection to the user.
* @return the order collection.
*/
public ArrayList getOrder()
{
return order;
}
/**
* Add an item to the order.
* @param OrderedItem the item to be added
*/
public void addOrderItem(OrderedItem foodItem)
{
order.add(foodItem);
}
public void removeOrderItem(ArrayList<String> itemArray)
{
//**We don't know what to put here!**
}
}
if you need to remove multiple items, you can add them to a temporary list and use removeAll too 如果您需要删除多个项目,则可以将它们添加到临时列表中,也可以使用removeAll
// creating ArrayList
java.util.List<String> list = new ArrayList<>();
list.add("apple");
list.add("banana");
list.add("mango");
list.add("barry");
java.util.List<String> needToRemove = new ArrayList<>();
needToRemove.add("apple");
needToRemove.add("banana");
// removeAll will remove multiple elements,
// We can pass any collection like Set, List or any other
list.removeAll(needToRemove);
System.out.println("After Removed From List:");
list.forEach(System.out::println);
Arraylist.clear(); Arraylist.clear(); method can be used to clear the array list Arraylist.removeAll();
该方法可用于清除数组列表Arraylist.removeAll();。 method removes all the elements of the array list.
方法删除数组列表中的所有元素。 Both the methods clear the elements of the list.
两种方法都清除列表的元素。 The clear() method has a better performance due to the less number of lines it has to execute.
由于必须执行的行数较少,clear()方法具有更好的性能。 This link https://howtodoinjava.com/java/collections/arraylist/empty-clear-arraylist/ provides the source codes for the two methods
此链接https://howtodoinjava.com/java/collections/arraylist/empty-clear-arraylist/提供了这两种方法的源代码
很好的朋友,只要您有对象的密钥(即安全密钥.remove()
,就可以尝试使用.remove()
方法
As I see the array list that you pass to removeOrderItem
method is a list of strings and your array list in the class is a list of OrderItem
. 如我所见,传递给
removeOrderItem
方法的数组列表是字符串列表,而类中的数组列表是OrderItem
列表。 So first you must find what is common between these two lists. 因此,首先您必须找到这两个列表之间的共同点。
Let's assume the model of OrderItem
has a field that contains the name of each order ( orderItem.getName()
). 假设
OrderItem
模型的字段包含每个订单的名称( orderItem.getName()
)。 And the itemArray
in the removeOrderItem
method is a list that contains names of some orders that you want to remove them. 而
itemArray
在removeOrderItem
方法是包含一些订单要删除它们的名称的列表。
Ok, in this case, we can do this: 好的,在这种情况下,我们可以这样做:
public void removeOrderItem(ArrayList<String> itemArray) {
List<OrderedItem> tempList = new ArrayList<>();
for (OrderedItem food: this.order) {
for (String foodName: itemArray) {
if (food.getName().equals(foodName))
tempList.add(food);
break;
}
}
this.order.removeAll(tempList);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.