Java – 检查 arrayList 中的重复项并阻止用户将订单添加到数组中

huangapple go评论60阅读模式
英文:

Java - Checking for duplicates in arrayList and stop user from adding an order to the array

问题

一个图形用户界面程序,当用户尝试添加重复订单时,在顶部显示标签:“订单未添加 - 重复”。

为了将订单列表中的订单视为重复,所有文本字段中的输入(当按钮被点击时)都必须相同。

我认为我需要使用一个for循环来检查订单是否已经在ArrayList中,但我不太确定如何操作,我对Java还不太熟悉。

英文:

A GUI program that displays a label at the top with "Order not added -duplicate" when a duplicate order is attemped to be added by the user.

for the order in orderList to be considered duplicate all inputs in textfields (when the button is clicked) have to be the same.

I suppose that I have to use a for loop to check if the order is already in the arrayList but I'm not sure how, I'm pretty new to java.

答案1

得分: 2

代替数组,你应该使用 Set,它是一种专门设计用来防止重复元素的集合:

Set 是一种不能包含重复元素的集合。它模拟了数学中的集合抽象概念。Set 接口只包含从 Collection 继承的方法,并且添加了禁止重复元素的限制。Set 也在 equals 和 hashCode 操作的行为上添加了更强的约定,允许即使实现类型不同,Set 实例也能有意义地进行比较。如果两个 Set 实例包含相同的元素,则它们是相等的。

由于你对 Java 语言还不熟悉,先退后一步,首先学习一下标准库提供的基本集合:ListSetMap。一旦你理解了这些抽象以及它们的实现变体,你就能够解决许多常见的日常问题。

英文:

Instead of an array you should be using a Set which is a collection specifically designed to prevent duplicates:

> A Set is a Collection that cannot contain duplicate elements. It models the mathematical set abstraction. The Set interface contains only methods inherited from Collection and adds the restriction that duplicate elements are prohibited. Set also adds a stronger contract on the behavior of the equals and hashCode operations, allowing Set instances to be compared meaningfully even if their implementation types differ. Two Set instances are equal if they contain the same elements.

Since you are new to Java language take a step back and first learn about basic collections provided by the standard library: List, Set and Map. Once you understand this abstractions and their implementation variants you will be able to solve a lot of common day-to-day problems.

答案2

得分: 0

在实际场景中,使用Set会是一个不错的选择,但考虑到这似乎是一个带有任意限制的作业任务,我们来看看如何使用ArrayList来完成。

首先,我建议您在Order对象上重写.equals()方法以正确比较字段的值。类似这样...

@Override
public boolean equals(Object obj) {
  //同一引用意味着在所有情况下都是相同的对象
  if(obj == this)
    return true;
  //未实例化或不是相同的类,因此显然不相等
  if(obj == null || obj.getClass() != getClass())
    return false;

  //将其转换为相同的对象类型
  Order o = (Order) obj;
  //开始比较每个变量,如果任何一个不匹配,就会返回false
  //请确保对于任何变量对象(例如String),都使用equals操作
  return this.name.equals(o.name) && this.value == o.value && this.someVar == o.someVar; //等等...
}

然后,您只需要将字段转换为正常的对象...

Order order = new Order(field1.getText(), field2.getText(), field3.getText()); //等等...

并在与现有值进行比较时运行它。我建议为此制作一个单独的方法...

public static boolean orderExists(Order order) {
  for(Order otherOrder : orderArrayList) {
    if(order.equals(otherOrder))
      return true;
  }
  return false;
}

从这里开始,orderExists() 方法将告诉您是否已经存在,您可以实现显示错误的代码。希望这能帮助您建立足够的设置,以便能够自行处理其余部分!

英文:

While a Set would be good in a real world scenario, this appears to be a homework assignment with arbitrary limitations so let's see how it can be done using an ArrayList.

First thing I would recommend doing is overriding your .equals() method on your Order object to properly compare field values. Something like...

@Override
public boolean equals(Object obj) {
  //Same reference so same object in every sense of the word
  if(obj == this)
    return true;
  //Not instanced nor the same class so clearly not equal
  if(obj == null || obj.getClass() != getClass())
    return false;

  //Cast it to the same object type
  Order o = (Order) obj;
  //Start comparing every variable, will return false if any of them don't match
  //Be sure to use the equals operation for any variable objects such as String
  return this.name.equals(o.name) && this.value == o.value && this.someVar == o.someVar; //ect...
}

Then you just need to take your fields and turn it into a object like normal...

Order order = new Order(field1.getText(), field2.getText(), field3.getText()); //ect...

And run it in a compare against the existing values. I recommend making a separate method for this...

public static boolean orderExists(Order order) {
  for(Order otherOrder : orderArrayList) {
    if(order.equals(otherOrder))
      return true;
  }
  return false;
}

From there, the orderExists() method will tell you if it's already in there and you can implement your code for displaying the error. Hope this helps you get set up enough to handle the rest on your own alright!

huangapple
  • 本文由 发表于 2020年10月9日 02:31:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/64268632.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定