gpt4 book ai didi

java - 使用 java.util.Map 时出现编译问题

转载 作者:行者123 更新时间:2023-12-01 07:42:01 25 4
gpt4 key购买 nike

我正在尝试编译这个程序。它非常适合 2 个字符串(姓名、电话号码),但不适用于 3 个字符串(姓名、电话号码和性别)。

<小时/>

代码(无效代码 - 3 个字符串(姓名、电话号码和性别))

<小时/>
import java.util.Map;
import java.util.TreeMap;

public class Ann {

String name, phone;

public Ann() {
}

public static void testMap() {
Map<String, String, String> theMap = new TreeMap<String, String,String>();
// new HashMap<K,V>(); could also be used
theMap.put("Roger M", "090-997-2918", "Male");
theMap.put("Jane M", "090-997-1987", "FeMale");
theMap.put("Stacy K", "090-997-9188", "FeMale");
theMap.put("Gary G", "201-119-8765", "Male");
theMap.put("Jane M", "090-233-0000", "FeMale");
System.out.println("Testing TreeMap and Map");
System.out.print("Stacy K has phone ");
System.out.print(theMap.get("Stacy K"));
System.out.print("\n");

System.out.print("Jane M has phone ");
System.out.print(theMap.get("Jane M"));
} // testMap()

public static void main(String[] args) {
testMap();

}
}
<小时/>

错误

wrong number of type arguments; required 2

wrong number of type arguments; required 2
<小时/><小时/>

工作代码(对于 2 个字符串(姓名、电话号码))

<小时/>
import java.util.Map;
import java.util.TreeMap;

public class Ann {

String name, phone;

public Ann() {
}

public static void testMap() {
Map<String, String> theMap = new TreeMap<String, String>();
// new HashMap<K,V>(); could also be used
theMap.put("Roger M", "090-997-2918");
theMap.put("Jane M", "090-997-1987");
theMap.put("Stacy K", "090-997-9188");
theMap.put("Gary G", "201-119-8765");
theMap.put("Jane M", "090-233-0000");
System.out.println("Testing TreeMap and Map");
System.out.print("Stacy K has phone ");
System.out.print(theMap.get("Stacy K"));
System.out.print("\n");

System.out.print("Jane M has phone ");
System.out.print(theMap.get("Jane M"));
} // testMap()

public static void main(String[] args) {
testMap();

}
}
<小时/>

我希望代码适用于大约 5 个属性,例如姓名、电话、性别、年龄、地址。如果有人可以帮我编译问题顶部的代码,我就可以弄清楚剩下的事情。

谢谢

最佳答案

您不能随意向泛型类型添加类型参数 - 它们是用一定数量定义的,并且必须使用那么多(不考虑原始类型)。类型参数对于实现有特定的含义 - HashMap 会如何?如果您调用map.get(name),类(class)知道您想得到什么?

您应该将所有属性封装到一个类中(例如 PersonContact ),然后创建一个 Map<String, Person>从名称映射到人。例如:

public enum Gender
{
FEMALE, MALE;
}

public final class Person
{
private final String name;
private final Gender gender;
private final Date dateOfBirth;
private final String address;
private final String telephone;

public Person(String name, Gender gender, Date dateOfBirth,
String address, String telephone)
{
// You probably want to put some validation in here
this.name = name;
this.gender = gender;
this.dateOfBirth = dateOfBirth;
this.address = address;
this.telephone = telephone;
}

public String getName()
{
return name;
}

// etc for the other properties
}

...

Map<String, Person> map = new HashMap<String, Person>();
Person jon = new Person("Jon", Gender.MALE, /* etc */);
map.put("Jon", jon);

关于java - 使用 java.util.Map 时出现编译问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3218361/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com