8 Replies - 239 Views - Last Post: 22 February 2013 - 10:34 AM Rate Topic: -----

#1 InVaSiOn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 22-February 13

Circular Single Linked List compile error

Posted 22 February 2013 - 02:43 AM


public class CircularSLList<T>
{
	protected class SLNode<T>
	{
		public T element = null;
		public SLNode<T> next = null;
	}
	
	protected SLNode<T> tail = null;
	protected SLNode<T> currentNode = null;
	int size;
	
	@Override
	public String toString()
	{
		SLNode<T> current = tail;
		if(tail == null)
			return "<Empty List>";
		else
		{
			String s = "<";
			for(int i = -1; i < size; i++, current=current.next)
			{
				if(i > -1)
					s = s.concat(current.element.toString() + " ");
			}
			s = s.concat(">");
			s = s.concat(" tail = " + tail.element.toString());
			s = s.concat("; current = " + currentNode.element.toString());
			return s;
		}
	}
	
		
	
	
	
	public T get()
	{
		return null;
	}
	
	
	public T find(T element)
	{
		return null;
	}
	
	
	public void addToTail(T element)
	{
		if (isEmpty())
		{
			tail = new SLNode<T>(element);
			tail.next = tail;
		}
		else
		{
			//tail.next = new SLNode<T>(element,tail.next);
			//tail = tail.next;
		}
	}
	
	public void addToHead(T element)
	{
	
	}
	

	public T removeFromTail()
	{
		return null;
	}
	

	public T removeFromHead()
	{

		return null;
	}
	
	
	public boolean isEmpty()
	{
		return (tail == null);
	}
}




Hi I'm new to Java and can't figure out how to fix this compile error.

error: constructor SLNode in class CircularSLList<T#1>.SLNode<T#2> cannot be applied to given types;
tail = new SLNode<T>(element);
^
required: no arguments
found: T#1
reason: actual and formal argument lists differ in length
where T#1,T#2 are type-variables:
T#1 extends Object declared in class CircularSLList
T#2 extends Object declared in class CircularSLList.SLNode
1 error

Is This A Good Question/Topic? 0
  • +

Replies To: Circular Single Linked List compile error

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2194
  • View blog
  • Posts: 5,222
  • Joined: 10-September 10

Re: Circular Single Linked List compile error

Posted 22 February 2013 - 03:22 AM

The statement:

tail = new SLNode<T>(element);

requires an SLNode<T> constructor that accepts an argument of type T. The class SLNode<T> currently only has the implied default constructor that accepts no arguments. Either remove the argument, 'element', or add a constructor to the class with the required parameter.
Was This Post Helpful? 1
  • +
  • -

#3 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5643
  • View blog
  • Posts: 12,359
  • Joined: 16-October 07

Re: Circular Single Linked List compile error

Posted 22 February 2013 - 05:55 AM

Why all the protected? Also, you really don't need to define the <T> for the internal node. It knows about the T from the parent. e.g.
public class CircularSLList<T> {
	private class SLNode {
		public T element;  // no  = null, use a constructor
		public SLNode next;
		public SLNode(... your constructor here
	}

	// confusing
	// protected SLNode<T> tail = null;
	// why?
	// protected SLNode<T> currentNode = null;
	// no
	// int size;
	
	private SLNode head;
	public  CircularSLList() { // constructors are your friends
		this.head = null;
	}

	// perfectly good method
	public boolean isEmpty() { return head == null; }

	
	@Override
	public String toString() {
		// use isEmpty
		if (isEmpty()) { return "<Empty List>"; }

		// consider a string builder



Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#4 InVaSiOn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 22-February 13

Re: Circular Single Linked List compile error

Posted 22 February 2013 - 07:07 AM

Thanks that helped!

Just another question. In the driver program when creating an object for testing for instance, int values when compiling an error occures:

CircularSLList<int> list = new CircularSLList<int>();
^
required: reference
found: int


But when it's sting values it can compile without error.

CircularSLList<String> list = new CircularSLList<String>();

Why is this?
Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5643
  • View blog
  • Posts: 12,359
  • Joined: 16-October 07

Re: Circular Single Linked List compile error

Posted 22 February 2013 - 08:28 AM

That's a little bit of Java badness, there. Java's generics are rather brain dead in spots.

In particular, the type can't be a primitive. Happily, every primitive does have a class wrapper. So, instead of int.
CircularSLList<Integer> list = new CircularSLList<Integer>();



Due to some overloading magic, you can generally treat this as if it was int when you use your list.
Was This Post Helpful? 1
  • +
  • -

#6 InVaSiOn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 22-February 13

Re: Circular Single Linked List compile error

Posted 22 February 2013 - 08:40 AM

View Postbaavgai, on 22 February 2013 - 05:28 PM, said:

That's a little bit of Java badness, there. Java's generics are rather brain dead in spots.

In particular, the type can't be a primitive. Happily, every primitive does have a class wrapper. So, instead of int.
CircularSLList<Integer> list = new CircularSLList<Integer>();



Due to some overloading magic, you can generally treat this as if it was int when you use your list.


Thanks!
Was This Post Helpful? 0
  • +
  • -

#7 InVaSiOn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 22-February 13

Re: Circular Single Linked List compile error

Posted 22 February 2013 - 09:42 AM

public class RunList 
{
    public static void main(String[] args) 
    {
         CircularSLList<Integer> list = new CircularSLList<Integer>();
			list.addToTail(5);
			list.addToTail(52);
			list.addToTail(33);
			list.addToHead(4);
   
			list.toString();
    }
}




When I run the main I get an error implementing my toString method.

Exception in thread "main" java.lang.NullPointerException
at CircularSLList.toString(CircularSLList.java:28)
at RunList.main(RunList.java:13)

Am I using the toString in the wrong way?
Was This Post Helpful? 0
  • +
  • -

#8 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5643
  • View blog
  • Posts: 12,359
  • Joined: 16-October 07

Re: Circular Single Linked List compile error

Posted 22 February 2013 - 10:05 AM

Without seeing the code I couldn't begin to tell you was the problem is. It shouldn't fail for what your'e doing, but it also wouldn't give you any output.

You'd want:
System.out.println(list.toString());



Or, to implicitly call toString ( more Java magic ), just:
System.out.println(list);


Was This Post Helpful? 1
  • +
  • -

#9 InVaSiOn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 22-February 13

Re: Circular Single Linked List compile error

Posted 22 February 2013 - 10:34 AM

Thanks that solved it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1