0

I am trying to print out the elements in my ArrayList that looks like

static ArrayList<LinkedList> listy = new ArrayList<>();

I tried to create a function called PrintTest()

public static void pTest() {
String [] top;
for (LinkedList i: listy) {
    
    //i.show();
    System.out.println(i.toString());

However, I am still getting when I call printTest()

LinkedList@15975490
LinkedList@6b143ee9
LinkedList@1936f0f5
LinkedList@6615435c
LinkedList@4909b8da
LinkedList@3a03464
LinkedList@2d3fcdbd
LinkedList@617c74e5

Do I need to iterator over this once more? I am confused on how to go about this. Can I override toString()? I can't seem to get it to work

Here is my linkedlist implementation code

public class LinkedList {
    Node head;
    Node tail;
    
    
    public String getFirst() {
        Node node = head;
        if (node.next == null) {
            throw new NoSuchElementException();
        }
        else {
        return node.data;
        }
        
    }
    
    public void insert(String data) {
        Node node = new Node();
        node.data = data;
        node.next = null;
        
        if (head == null) {
            head = node;
            
        }
        else {
            Node n = head;
            while(n.next !=null) {
                n = n.next;
                
            }
            n.next = node;
        }
    }
    
    public void insertAtStart(String data) {
        Node node = new Node();
        node.data = data;
        node.next = null;
        node.next = head;
        head = node;
        
    }
    
    public void insertAt(int index, String data) {
        Node node = new Node();
        node.data = data;
        node.next = null;
        
        if(index == 0) {
            insertAtStart(data);
        }
        else {
            Node n = head;
            for (int i = 0; i < index-1; i++) {
                n = n.next;
            }
            node.next = n.next;
            n.next = node;
        }
    }
    
    public void deleteAt(int index) {
        if (index == 0) {
            head = head.next;
        }
        else {
            Node n = head;
            Node n1 = null;
            for (int i = 0; i < index-1; i++) {
                n = n.next;  
            }
            n1 = n.next;
            n.next = n1.next;
            //System.out.println("n1 " + n1.data);
            n1 = null;
        }
    }
    
    public int size() {
        int count =0;
        Node pos = head;
        while (pos != null) {
            count++;
            pos = pos.next;
        }
        return count;
    }
    
    
    public void remove(String s) {
        Node node = head;
        while (!node.data.equals(s)) {
            node = node.next;
        }
        if (node.next == null) {
            node.data = null;
        }
        else {

            node.data = node.next.data;
            node.next = node.next.next;
        }

                //System.out.println("n1 " + n1.data);
    }
    
    public void show() {
        Node node = head;
        while(node.next != null) {
            System.out.println(node.data);
            node = node.next;
            
        }
        System.out.println(node.data);
harry
  • 9
  • 2
  • 1
    When you create your own `LinkedList` impl, then you need to overwrite `toString()` or inherit it from somewhere. – Tom Jul 20 '21 at 03:30
  • It is printing hashcode value of the element. What is testy if your arraylist is listy? – Vishwajit Rahatal Jul 20 '21 at 03:33
  • What exactly is `LinkedList`? Apparently not [`java.util.LinkedList`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/LinkedList.html). If you are using another such class from another 3rd-party library, say so. If you are writing your own class, show the source code. Voting to close as there is not enough detail to justify an Answer. – Basil Bourque Jul 20 '21 at 03:47
  • @VishwajitRahatal I fixed it i am sorry. – harry Jul 20 '21 at 03:51
  • @Basil Bourque. I coded my own version of LinkedList to help me understand how it works and I am using that – harry Jul 20 '21 at 03:51

1 Answers1

2

In java, every class inherits the Object class. In the Object class default definition for the toString method is hashcode. When you are calling toString method, you need to define by yourself.

class LinkedList{
    int value;

    @Override
    public String toString(){
        return String.valueOf(value);
    }
}

I think you can go through this article to get more understanding.Explanation For toString

Vimit Dhawan
  • 597
  • 1
  • 7
  • 25