private Node<T> addAt(Node<T> node, T value) {
	if (node == null) {	// special case
	    return new Node<T>(value, null);
	} else if (node.getNext() == null) { // other special case
	    node.setNext(new Node<T>(value, null));
	} else if (node.getstuff().getName().compareTo(value.getName()) > 0)        {
	    node.setNext(new Node<T>(value, node.getNext()));
	} else
	    addAtEnd(node.getNext(), value);
	}
	return node;
    }

so my problem is that im trying to call a method of the object in a particular node, but it keep telling me that the method inside my object class is not recognized, cannot find symbol, in this case, the getName method, any ideas?

Look at the source code of the existing collection classes in Java to see how you can implement your own generic collection type. Also, read my reply here.

I have the same exact problem. But i don't understand the link you gave me. I couldn't call a method of the object in a node. What is the problem? How can I call the method?

Post a "compilable" and "short" piece of code which demonstrates your problem.

...

data class

public class Data
{
    String name;
    int age;
    
    
    Data (String name, int age)
    {
        this.name = name;
        this.age = age;
    }
    
    Data (Data e)
    {
        this.name = e.name;
        this.age = e.age;
    }

    public String getName()
    {
        return name;
    }

    public int getAge()
    {
        return age;
    }
    
    public String toString()
    {
        return name + age;
    }
}

node class

public class Node
{
    Object item;
    Node link;
    
    Node (Object item)
    {
        this.item = item;
        link = null;
    }
    
    Object getItem()
    {
        return item;
    }
    
    Node getLink()
    {
        return link;
    }
}

list(linked list) class

public class List
{
    Node head;
    
    List()
    {
        head = null;
    }
    
    void addTop (Object n)
    {
        Node newnode = new Node(n);
        newnode.link = head;
        head = newnode;
    }
    
    void display()
    {
        Node current = head;
        
        while (current != null)
        {
            System.out.println(current.item);
            current = current.link;
        }
    }
}

client class

public class Client
{
    public static void main (String args[])
    {
        List list = new List();
        Data item1 = new Data ("Jack",10);
        Data item2 = new Data ("Rose",25);
        
        list.addTop(item1);
        list.addTop(item2);
        
        list.display();
    }
}

I want to read the name and age from the data I stored in the linkedlist. I tried to call method getName and getAge but it gives me an error.

This is because the getObject() methods returns an Object and the Object class doesn't have the methods `getName` and `getAge`. You either need to cast the object returned by the `getObject` method to `Data` or "generify" your List class so that it accepts a type parameter just like the ArrayList class of JDK.

Which way would be easier for me? And can you please explain how should I cast the object or how to generify my list class?

Umm. You are programming in Java and don't understand how to do a cast? Try looking for cast or typecast in the index of your text. This will be the fastest fix because it is the easiest to understand. "Generifying" your collection would be the better long term fix, if: You know you are going to always be holding instances of Data and you want to avoid writing casts "everywhere".

Thanks. I solved it by casting the object.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.