0

more than just one variable in a node?

hello, I have been sent to make a payroll in java using the linkedlist, but I have a question, if I can add the payroll data that they are; name, ID, telephone number, etc ... in a single node? ... so when he asks for the name of the worker that indicates all the data of this, it is necessary to clarify that it is without gui

19th Feb 2019, 4:24 PM
Sebastian Torres
Sebastian Torres - avatar
1 Réponse
0
Well, you can't use more then one data type in a single node, but you can aggregate all of those data types into a class, and then create nodes of that class. Here's an example of what that might look like: class Person { String name; String telephoneNumber; int id; //add other data that you need for the person in here Person(String name, String number, int id) { this.name = name; this.telephoneNumber = number; this.Id = id; } //getters String getName() { return name; } int getId() { return id; } String getTelephoneNumber() { return telephoneNumber; } } You could then create a linked list using Java's built-in LinkedList template or using your own. LinkedList<Person> payroll = new LinkedList<Person>(); You can then query the list with the builtin LinkedList methods. For example, if you wanted to get the telephone number of the second employee, you would write: System.out.println(payroll.get(1).getTelephoneNumber()); Maybe you want to print out the Id of all of the people named "Alice". You could do something like this: for(Person p : payroll) { if(p.getName().equals("Alice")) System.out.println(p.getId()); } Hope this helped!
20th Apr 2019, 4:28 PM
Jack McCarthy
Jack McCarthy - avatar