954,523 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Generic LinkedList with T value as an object, problem calling method of obj

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?

blue6dImension
Newbie Poster
2 posts since Feb 2010
Reputation Points: 10
Solved Threads: 1
 

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 .

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

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?

bbman
Junior Poster
182 posts since May 2010
Reputation Points: 22
Solved Threads: 10
 

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

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

...

bbman
Junior Poster
182 posts since May 2010
Reputation Points: 22
Solved Threads: 10
 

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.

bbman
Junior Poster
182 posts since May 2010
Reputation Points: 22
Solved Threads: 10
 

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.

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

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?

bbman
Junior Poster
182 posts since May 2010
Reputation Points: 22
Solved Threads: 10
 

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".

griswolf
Veteran Poster
1,165 posts since Apr 2010
Reputation Points: 344
Solved Threads: 256
 

Thanks. I solved it by casting the object.

bbman
Junior Poster
182 posts since May 2010
Reputation Points: 22
Solved Threads: 10
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: