为什么我的 Set 在 Java 编程中充当数组列表?
各位程序员再次大家好。
所以我正在创建一个程序,允许用户指定他们想要的保险类型作为保险单的一部分。
由于只有 4 种类型的封面可供选择,因此我想使用集合或哈希集来跟踪向用户提供的封面类型。
问题是,它似乎没有消除重复项,而是将其视为数组列表,当我打印出集合的内容时,我得到了不应该在集合中发生的重复项。
这是我用来将创建的保险类型对象传递给保险单类的代码。在它们被实例化之后。
CoverType theInsuranceType = new CoverType("Fire",250);
theInsurancePolicy.includeCover(theInsuranceType);
这是我在保险单类中使用的代码,用于跟踪用户购买的保险类型。
import java.util.*;
// Class: InsurancePolicy
// Purpose: To represent a particular customer's insurance agreement
public class InsurancePolicy {
//ArrayList<Product> orderList = new ArrayList<Product>();
private static int totalPolicyCost;
private static String name = null;
private static int price = 0;
private int amount = 0;
private static int total = 0, claims = 0;
private static Set<CoverType> TheCoverType = new HashSet<CoverType>();
public static boolean includeCover(CoverType which)
{
TheCoverType.add(which);
System.out.println(which.getName()+" Has ben added to your insurance policy");
System.out.println(" ");
System.out.println(" ");
System.out.println("-----------------------");
return true;
}
我已经将其作为保险单类的一部分来完成,以迭代集合并为用户提取和打印每个对象的值,这就是我获取重复项的地方。
Iterator<CoverType> iter = TheCoverType.iterator();
int cash =0;
while (iter.hasNext())
{
CoverType type = iter.next();
cash = type.getPrice();
amount = amount + cash;
System.out.println("This one Cost. "+ cash);
// int cost = TheCoverType.getPrice().next();
// if theCover
}
amount = amount+ 100;
String message = "So all up total cost of the policy is. "+cash;
return amount;
任何帮助将不胜感激。
Hello again fellow programmers.
so I'm creating a program that allows users to specify the type of insurance cover they want as part of an insurance policy.
Since their is only 4 types of cover to choose from I want to use a set or hash set to keep track of the types of cover offered to users.
The problem is that is does not seem to get rid of duplicates it is treating it like an arraylist and when I print out the contents of the set i get duplicates which should not happen in a set.
This is the code I used to pass the created insurance type objects to the insurance policy class.After they have been instantiated.
CoverType theInsuranceType = new CoverType("Fire",250);
theInsurancePolicy.includeCover(theInsuranceType);
and this is the code I used in the insurance policy class to keep track of the type of insurance that a user has taken out.
import java.util.*;
// Class: InsurancePolicy
// Purpose: To represent a particular customer's insurance agreement
public class InsurancePolicy {
//ArrayList<Product> orderList = new ArrayList<Product>();
private static int totalPolicyCost;
private static String name = null;
private static int price = 0;
private int amount = 0;
private static int total = 0, claims = 0;
private static Set<CoverType> TheCoverType = new HashSet<CoverType>();
public static boolean includeCover(CoverType which)
{
TheCoverType.add(which);
System.out.println(which.getName()+" Has ben added to your insurance policy");
System.out.println(" ");
System.out.println(" ");
System.out.println("-----------------------");
return true;
}
I have done this as part of the the insurance policy class to iterate over the set and pull out and print values of each object for the user and that is where im getting the duplicates.
Iterator<CoverType> iter = TheCoverType.iterator();
int cash =0;
while (iter.hasNext())
{
CoverType type = iter.next();
cash = type.getPrice();
amount = amount + cash;
System.out.println("This one Cost. "+ cash);
// int cost = TheCoverType.getPrice().next();
// if theCover
}
amount = amount+ 100;
String message = "So all up total cost of the policy is. "+cash;
return amount;
Any help will be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
要放入集合中的对象需要实现 等于 和 哈希码。
即使所有字段都是“相等”,除非您重写这些方法,否则您也不会在 java 中获得对象相等 (
.equals()
)。Your objects that you want to put in a set need to implement equals and hashcode.
Even if all fields are "equal", you won't get object equality (
.equals()
) in java unless you override those methods.我假设该集合所持有的类型是“CoverType”,对吗?如果是这样,您是否为此类重载 equals 和 hashCode() ?
The type held by the set I assume is "CoverType", right? If so, do you overload equals and hashCode() for this class?
您是否为 CoverType 类实现了 .equals 和 .hashCode?
Have you implemented .equals and .hashCode for your CoverType class?