简体   繁体   English

如何关联多个链接列表中的已读用户输入?

[英]How do I associate read user inputs in multiple linked list?

So right now I'm writing a program that will take a user input such as: 因此,现在我正在编写一个程序,它将接受用户输入,例如:

  • "Jeff" for a first name “ Jeff”的名字
  • "Henderson" for a last name “ Henderson”的姓氏
  • a phone number with them 他们的电话号码
  • an amount to be stored in their bank account 存入其银行帐户的金额

And store it all into a Linked List. 并将其全部存储到链接列表中。

If I were to add another person with this similar info, how would that be stored in the link list such at I could access both individuals information separately? 如果我要添加具有该类似信息的其他人,该如何存储在链接列表中,例如我可以分别访问两个人的信息?

Right now I'm using this to establish all the storage when entering a new user: 现在,当我输入新用户时,我将使用它来建立所有存储:

private final Scanner keyboard;

private final LinkedList<String> firstName;
private final LinkedList<String> lastName;
private final LinkedList<String> teleNumber;
private final LinkedList<String> accountBalance;

public bankdata() 
{
    this.keyboard = new Scanner(System.in);
    this.firstName = new LinkedList<String>();
    this.lastName = new LinkedList<String>();
    this.teleNumber = new LinkedList<String>();
    this.accountBalance = new LinkedList<String>();
}

I plan to use keyboard.next(); 我打算使用keyboard.next(); to read the user input and then use an add method I've written to store the information into each linked list. 读取用户输入,然后使用我编写的add方法将信息存储到每个链接列表中。

The way it is set up is that there is a Linked List for every single object variable like: 设置方式是每个对象变量都有一个链接列表,例如:

  • a Linked List for first name 名字的链接列表
  • a Linked List for the last 最后一个链接列表
  • a Linked List for the tele number 电话号码的链接列表
  • a Linked List for the account balance 帐户余额的链接列表

If I want to call information associated with one specific person, how do I have it so when I request that users information that I can see all his stuff if they're in another linked list? 如果我想呼叫与一个特定人员相关的信息,当我要求用户提供我可以看到他所有的东西(如果他们在另一个链接列表中)的信息时,该怎么办?

The thing here is to create your own data structure. 这里要做的是创建自己的数据结构。 I'd say research a bit more about LinkedList. 我想说更多有关LinkedList的研究。 Almost every linked list example online can help you out. 几乎每个在线链接列表示例都可以为您提供帮助。

Hint: Create a class for your node, and store all information pertaining to each person in that node. 提示:为您的节点创建一个类,并存储与该节点中每个人有关的所有信息。

EDIT : Just to complete the answer: How do I create a Linked List Data Structure in Java? 编辑 :只是为了完成答案: 如何在Java中创建链接列表数据结构?

EDIT 2 : Based on your edited OP, I think you want to have 4 LinkedLists, and find all information for one person. 编辑2 :根据您编辑的OP,我想您想拥有4个LinkedList,并查找一个人的所有信息。 I'll assume you are going to search by the person's name (if not, the process is stil the same). 我假设您要按此人的姓名进行搜索(否则,搜索过程仍然相同)。 Also, it is assumed that information for one person is stored at the correct corresponding node. 另外,假定将一个人的信息存储在正确的对应节点处。 So, person1 has all information in first nodes of all 4 lists, person2 has all nodes at "index" 1. 因此, person1在所有4个列表的第一个节点中具有所有信息, person2在“ index” 1处具有所有节点。

So, you start searching the LinkedList storing names, and stop as soon as you find the name (or throw an error if not found). 因此,您开始搜索存储名称的LinkedList,并在找到名称后立即停止(如果找不到则抛出错误)。 Also keep track of the number of nodes you have seen. 还要跟踪您看到的节点数。 This will be the index you need to get information for all other Lists. 这将是您需要获取所有其他列表信息的索引。

Now, using for loop, traverse appropriate number of times on each of the rest 3 Lists, and return the value encountered. 现在,使用for循环,在其余3个列表中的每一个上遍历适当的次数,然后返回遇到的值。

It would be easier to group all that data in a class and then use a Hashtable as your data structure, you should find many resources online about Hashtable's also check out http://javarevisited.blogspot.com/2012/01/java-hashtable-example-tutorial-code.html . 将所有数据分组到一个类中然后使用Hashtable作为您的数据结构会更容易,您应该在线上找到许多有关Hashtable的资源,还可以查看http://javarevisited.blogspot.com/2012/01/java-hashtable -example-tutorial-code.html About obtaining input..you can store it in an array since the number of variables is fixed then use Arrays.toList() to cast it into a list and perhaps use a switch inside the loop to initialize your variables. 关于获取输入..您可以将其存储在数组中,因为变量的数量是固定的,然后使用Arrays.toList()将其转换为列表,并可能使用循环内的开关来初始化变量。 note that there are alternatives to the hashtable structure but i recomend since you want to retrieve your data later and it might get large. 请注意,哈希表结构还有其他选择,但我建议您这样做,因为您想稍后再检索数据,并且它可能会变大。

This is a typical Primitive Obsession . 这是典型的原始痴迷

You should use an Object to save the data. 您应该使用对象来保存数据。 It would be more flexible and easy to use. 它将更加灵活并且易于使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何创建用户输入列表? - How do I create a list of user inputs? 如何使用 JOptionPane 和数组由用户制作多个输入的列表? - How do I make a list of multiple inputs by the user by using JOptionPane and arrays? 如何在文本字段中添加用户输入列表 - How do I add a list of user inputs into a textfield 我如何向用户询问字符串输入? (如果他们想输入中心和半径,或直径的端点?)(阅读输入) - How do I ask the user for String inputs ? (if they want to enter center and radius, or endpoints of diameter?) (Read an input) 如何将数字与数组列表关联? - How do I associate numbers with my array list? 如何从用户读取多个输入以在Java中进行数组操作 - How to read multiple inputs from user for array manipulation in java 如何获得多个客户端可以读写的链接列表? - how can I have a linked list that multiple clients can read write to? 如何使用Java从链接列表中的文本文件中读取字符串? - How do I read strings from a textfile in Linked List usings Java? 如何从用户在Java中输入的文件中获取数字列表? - How do I get the list of numbers off of a file that the user inputs in Java? 当涉及聚合类时,如何将用户输入输入到数组列表中? - How do I get user inputs into an array list when there is an aggregate class involved?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM