0

I created a Person Object that takes a string and an integer. I created a Node class to take in type generic as input. When I debug, I can see the objects are created, and stored, but my output is just memory addresses of said so objects. I tried parsing my objects to strings in my toString() method, but no luck. I have a hunch that the issue is the way I am declaring the generic datatype. Here is my code:

Class Person

public class Person {
   //variables
   private String name;
   private int age;

   // default const.
   public Person() {
      name = " ";
      age = 0;
   }
   // overloaded constructor
   public Person(String name, int age) {
      this.name = name;
      this.age = age;
   }
   // methods
   public void setName(String name) {
      this.name = name;
   }
   public void setAge(int age) {
      this.age = age;
   }
   public String getName() {
      return name;
   }
   public int getAge() {
      return age;
   }
}

Class Node

package LinkedList;


public class Node <T> {
   // added parameters for person object
   T p;

   Node nextNode;

   public Node () {

   }
   public Node (T p) {
      this.p = p;

   }

   // parse int data into string to simplify the unit test by not showing node memory locations
   @Override
   public String toString() {
      return p.toString();
   }
}

Linked List class

package LinkedList;

public class myLinkedList <T>{
   //Nodes
   Node head;
   Node tail;
   int size = 0;

   // Generic method
   public void add (T p){
      Node node = new Node(p);
      if(tail == null){
         head = node;        // if no previous tail the new node is the head
         tail = node;        // if no previous tail the new node is also the tail
      }else {
         tail.nextNode = node;  // adds a node to the end of the list
         tail = node;           // the newest node is set to the tail;
      }
      size ++;
   }

   // Generic search method
   public Node search (T p) {
      //empty list
      if (head == null)
         return null;

      // check if the first node is a match
      if (head.p == p)
         return head;

      // assign node as iterator
      Node node = head;

      // iterate through linked list
      while (node.nextNode != null) {
         // assign the next node as current node
         node = node.nextNode;
         // compare current node's data to data
         if (node.p == p)
            System.out.println(" object found");
         return node;
         //exit when the last node is reached

      }
      return null;
   }

   public Node findPreviousNode (T p) {
      // if the current node has no previous node (it's the head)
      if(head.p == p)
      return new Node();

      Node node = head;
      //go through list looking for desired node and return the one before it
      while(node.nextNode != null) {

         if(node.nextNode.p == p)
         return node;
         // if desired node is not found move onto next node
         node = node.nextNode;

      }
      // returns null for previous node if desired node doesn't exist
      return null;
   }

   public Node delete (T p){

      // node selected to delete, initializes as null for a placeholder
      Node nodeToReturn = null;

      //empty list nothing to return
      if(size == 0)
      return null;

      //list of only one node
      if(size == 1) {
         nodeToReturn = head;
         head = null;
         tail = null;
         size --;
         return nodeToReturn;
      }

      // find and delete last node
      Node nodePrev = findPreviousNode(p);

      if(nodePrev.p == null) {

         head = head.nextNode;
         size --;

      } else if(nodePrev != null) {
         if(tail.p == p) {
            nodePrev.nextNode = null;
            tail = nodePrev;
         } else {

            nodePrev.nextNode = nodePrev.nextNode.nextNode;
         }
         size --;
      }

      return null;

   }

   public void traverse() {
      // checks for empty list and prints out the first node
      if(head != null) {
         Node node = head;
         System.out.print(node);

         // moves from node to node printing until it reaches null as nextNode
         while(node.nextNode != null) {

            node = node.nextNode;
            System.out.print( "  " + node.toString());
         }
      }
   }
}

Main Class

import java.util.ArrayList;
import LinkedList.*;

public class main {
   public static void main(String[] args) {
      // implement linked list
      System.out.println("\n\n Object Linked list");
      myLinkedList peopleGroup = new myLinkedList();
      peopleGroup.add(new Person("robert", 23));
      System.out.println(peopleGroup.search("robert"));
      peopleGroup.add(mike);
      peopleGroup.add(marie);

      peopleGroup.traverse();
   }
}

Output: (I am guessing, same reason the search function is returning null is the culprit).

Object Linked list
null
Person@677327b6  Person@14ae5a5  Person@7f31245a
Process finished with exit code 0
Gaurav Jeswani
  • 4,410
  • 6
  • 26
  • 47
hyperlyght
  • 3
  • 1
  • 5

3 Answers3

0

If you want more meaningful strings you need to override toString in your Person class.

swpalmer
  • 3,890
  • 2
  • 23
  • 31
  • Ok, so add a toString() to the person class instead of having it in the Node class? Or in addition to (having it in both classes). – hyperlyght Sep 14 '19 at 02:15
0

The output you get are not memory addresses but hashes.

The default implementation of toString() is ClassName@hashValue.

Now you are overriding toString() in class Node to return the value of Person.toString() which is still the default implementation.

So if you now override toString() in class Person you are good to go.

LBPFR34K
  • 56
  • 1
  • 8
0

In your code you have few issues:

For match you should not dependent of toString method.

In the search method you are passing input as String (name). And to consider it a match you have conditions like if (head.p == p) and if (node.p == p), where Node is a generic type(compile type), but technically it is Person object at runtime. So here you are comparing String with Person Object. Which is never going to give you a match at all.

If you want to match it, you need to override the equals method of Person Class as well as hashcode. As below:

public boolean equals(Object obj) 
{
  if (this == obj) return true;
    if (obj == null) return false;
    if (this.getClass() != obj.getClass()) return false;
    Person that = (Person) obj;
    if (this.age != that.getAge()) return false;
    if (!this.name.equals(that.getName())) return false;
    return true;
}

@Override
    public int hashCode() {
        int result = 17;

        result = 31 * result + name.hashCode();
        result = 31 * result + age;
        return result;
    }

You can override these method according to your requirement and criteria of search. But along that you need to change code in search method as below :

// Generic search method
   public Node search (T p) {
      //empty list
      if (head == null)
         return null;

      // check if the first node is a match
      if (head.p.equals(p))
         return head;

      // assign node as iterator
      Node node = head;

      // iterate through linked list
      while (node.nextNode != null) {
         // assign the next node as current node
         node = node.nextNode;
         // compare current node's data to data
         if (node.p.equals(p))
            System.out.println(" object found");
         return node;
         //exit when the last node is reached

      }
      return null;
   }

And when you are passing Person from search, you need to pass the Person Object as below:

peopleGroup.search(new Person("robert", 23));

So after these changes you will get result, something like (Skipped adding mike and marie):

Object Linked list

test.file1.Person@518532a6

test.file1.Person@518532a6  test.file1.Person@6319ab2  test.file1.Person@bf8ccc2e
Gaurav Jeswani
  • 4,410
  • 6
  • 26
  • 47
  • Thank you. After your comment I did notice I was passing a string instead of passing the name of an object to search. I changed my call of search to just pass in an object's name ex. System.out.println(peopleGroup.search(robert)); – hyperlyght Sep 14 '19 at 04:19
  • So now, you will be getting correct result. – Gaurav Jeswani Sep 14 '19 at 04:21