java - 如何使用用户输入更改对象的名称?

标签 java list linked-list

我如何使用用户输入更改对象的名称。 对于前。我要求用户以字符串形式输入他们的 id。 我想用它来创建一个构造函数。

例如:

RefUnsortedList<Patients> NewList = new RefUnsortedList<Patients>();
Patients entry1=null;
System.out.println("Please enter patient's ID: ");
            String TargetID = scan.next();

我要设置

Patients entry1 = null;

做到这一点

Patients "the user input here" = null;

最佳答案

java中没有动态变量,必须在源代码中声明。

您可以尝试使用 Map 并为变量的每个实例分配一个键。

Map patientMap = new HashMap();
patientMap.put("entry1", new Patients());
patientMap.put("the user input here", new Patients());

然后,当您想要检索患者时,您可以使用:

Patients patient = patientMap.get("the user input here");

关于java - 如何使用用户输入更改对象的名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22915233/

相关文章:

java - 在 Java 中使用 Selenium WebDriver 下载 zip 文件 - 下载弹出窗口总是出现

java - 使用扫描仪读取多个位

python - 如何从 CSV 文件中读取包含逗号的列表作为一列?

c# - 如何按字母顺序对字母数字列表进行排序

c - 删除链表所有节点

c++ - 双向链表 : Properly deleting an adding something in the middle of a list?

java - 用gradle文件同步项目时出现问题

java - 从 DUAL : MySQL 中选择 1

c# - c# 容器中的 push_back 操作

javascript - 插入链表得到TypeError : Cannot set property 'next' of undefined