gpt4 book ai didi

java - 创建无限的字典或列表

转载 作者:行者123 更新时间:2023-12-02 03:57:25 25 4
gpt4 key购买 nike

我需要获取用户输入来创建列表、元组、哈希表等以进行有序或 key 保存。例如,在 python 中,完成后将如下所示:

all = {'main1':{'name':'Hannag', 'birth':'date1', 'gender':'female'}, 'main2':{'name':'Kate', 'birth':'date2', 'gender':'female'}, 'main3':{'name':'Ben', 'birth':'date3', 'gender':'male'}}

在java中,我还没有弄清楚如何创建一个包含更多字符串键的字符串键。 main1':{'姓名':'汉纳格', '出生':'日期1', '性别':'女'}我尝试过使用元组,但您只能使用我尝试使用 tuple3 执行的一组数字。

        Map<String, Tuple3<String, String, String>> dos = new HashMap<String, Tuple3<String, String, String>>();
while(adds.contains(contin))
{
String li = input.nextLine();
String fName = input.nextLine();
String fDir = input.nextLine();

do.Add(fName, new Tuple3((String) li, (String) fName, (String) fDir));

我还尝试在哈希表中使用哈希表,但哈希表标题不能是字符串。我需要哈希表或列表等的名称是字符串。有什么办法可以解决这个问题吗?

最佳答案

不要乱搞元组和映射,而是创建一个类:

class Person {
final String name;
final String birth; // maybe make this a Date?
final String gender; // if you subscribe to the gender binary, maybe make this an enum?
Person(String name, String birth, String gender) { // constructor
this.name = name;
this.birth = birth;
this.gender = gender;
}
}

然后有一个

Map<String, Person> map = new HashMap<>();
map.put("main1", new Person("Hannag", "date1", "female"));
...

关于java - 创建无限的字典或列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35303376/

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