Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Newcomers » Newcomers » Array List problem(List is not generic it cannot be parameterized with arguments)
Array List problem [message #1021992] Thu, 21 March 2013 00:03 Go to next message
Andrew Chung is currently offline Andrew Chung
Messages: 4
Registered: March 2013
Junior Member
[ALIGN=left]I am having a compile problem with the following line:

DoublyLinkedList<Integer> list = new DoublyLinkedList<Integer>();

//Error here-> the type DoublyLinkedList is not generic it cannot be parameterized with arguments

I was reading some of the past posts but couldnt figure out what my problem was?

I am uploading my file because pasting in makes the formatting look strange.

Andrew
Re: Array List problem [message #1022204 is a reply to message #1021992] Thu, 21 March 2013 09:35 Go to previous messageGo to next message
Stephane Begaudeau is currently offline Stephane Begaudeau
Messages: 406
Registered: April 2010
Location: Nantes (France)
Senior Member

Hi,

Your class DoublyLinkedList is not generic but your class Node is, you need to put the generic "<E>" on DoublyLinkedList instead. You should have something like this:

package org.obeonetwork.pim.uml2.gen.java.services;

public class DoublyLinkedList<E> {

	class Node {

		// Node Data
		private E value;

		private Node prev;

		private Node next;

		// Node constructor
		Node(E value) {
			this.value = value;
		}

		Node(E value, Node prev, Node next) {
			this.value = value;
			setPrev(prev);
			setNext(next);
		}

		// Node Setter
		void setPrev(Node prev) {
			this.prev = prev;
		}

		void setNext(Node next) {
			this.next = next;
		}

		// Node Getter
		Node getPrev() {
			return prev;
		}

		Node getNext() {
			return next;
		}

		E getValue() {
			return value;
		}

		// <<List_data>>=
		private Node head = new Node(null);

		private Node tail = new Node(null);

		private int length = 0;

		// <<List_construct>>=
		{
			head.setPrev(null);
			head.setNext(tail);
			tail.setPrev(head);
			tail.setNext(null);
		}

		// <<List_get>>=
		public Node get(int index) throws IndexOutOfBoundsException {
			if (index < 0 || index > length) {
				throw new IndexOutOfBoundsException();
			} else {
				Node cursor = head;
				for (int i = 0; i < index; i++) {
					cursor = cursor.getNext();
				}
				return cursor;
			}
		}

		// <<List_remove>>=
		public E remove(int index) throws IndexOutOfBoundsException {
			if (index == 0) {
				throw new IndexOutOfBoundsException();
			} else {
				Node result = get(index);
				result.getNext().setPrev(result.getPrev());
				result.getPrev().setNext(result.getNext());
				length--;
				return result.getValue();
			}
		}

		// <<List_add>>=
		public void add(int index, E value) throws IndexOutOfBoundsException {
			Node cursor = get(index);
			Node temp = new Node(value);
			temp.setPrev(cursor);
			temp.setNext(cursor.getNext());
			cursor.getNext().setPrev(temp);
			cursor.setNext(temp);
			length++;
		}

		public void addHead(E value) {
			Node cursor = head;
			Node temp = new Node(value);
			temp.setPrev(cursor);
			temp.setNext(cursor.getNext());
			cursor.getNext().setPrev(temp);
			cursor.setNext(temp);
			length++;
		}

		public void addTail(E value) {
			Node cursor = tail.getPrev();
			Node temp = new Node(value);
			temp.setPrev(cursor);
			temp.setNext(cursor.getNext());
			cursor.getNext().setPrev(temp);
			cursor.setNext(temp);
			length++;
		}

		// <<List_utils>>=
		public int size() {
			return length;
		}

		public boolean isEmpty() {
			return length == 0;
		}

		@Override
		public String toString() {
			StringBuffer result = new StringBuffer();
			result.append("(head) - ");
			Node temp = head;
			while (temp.getNext() != tail) {
				temp = temp.getNext();
				result.append(temp.getValue() + " - ");
			}
			result.append("(tail)");
			return result.toString();
		}
	}

	// <<List_test>>=
	public static void main(String argv[]) {
		DoublyLinkedList<Integer> list = new DoublyLinkedList<Integer>(); // Error here-> the type
		// DoublyLinkedList is not generic
		// it cannot be parameterized with
		// arguments
		list.addHead(new Integer(1));
		list.addHead(new Integer(2));
		list.addTail(new Integer(9));
		list.addHead(new Integer(3));
		list.addTail(new Integer(11));
		list.add(2, new Integer(0));
		System.out.println(list);
		list.remove(list.size());
		System.out.println(list);
	}

}


Then you only have errors since the operations "addHead", "addTail", etc do not exists.

Regards,

Stephane Begaudeau, Obeo

--
Twitter: @sbegaudeau
Google+: +stephane.begaudeau
Blog: http://stephanebegaudeau.tumblr.com | Eclipse Java Development Tools Tips and Tricks



[Updated on: Thu, 21 March 2013 09:35]

Report message to a moderator

Re: Array List problem [message #1022508 is a reply to message #1022204] Thu, 21 March 2013 22:42 Go to previous messageGo to next message
Andrew Chung is currently offline Andrew Chung
Messages: 4
Registered: March 2013
Junior Member
Hi Stephane,
Many thanks for correcting me and replying so quickly. I am new to Java and learning about linked lists.

May i ask what is wrong with the following line below:

My main area looks like:

public static void main(String argv[]) {
DoublyLinkedList<Integer> list = new DoublyLinkedList<Integer>(); // Error here-> the type
// DoublyLinkedList is not generic
// it cannot be parameterized with
// arguments
list.addHead(new Integer(1)); // ERROR ->The method addHead(Integer) is undefined for the type DoublyLinkedList<Integer>
list.addHead(new Integer(2)); // ERROR ->The method addHead(Integer) is undefined for the type DoublyLinkedList<Integer>
list.addTail(new Integer(9)); // ERROR ->The method addTail(Integer) is undefined for the type DoublyLinkedList<Integer>
list.addHead(new Integer(3)); // ERROR ->The method addHead(Integer) is undefined for the type DoublyLinkedList<Integer>
list.addTail(new Integer(11)); // ERROR ->The method addTail(Integer) is undefined for the type DoublyLinkedList<Integer>
list.add(2, new Integer(0)); // ERROR ->The method add(Integer) is undefined for the type DoublyLinkedList<Integer>
System.out.println(list);
list.remove(list.size()); //ERROR ->The method size() is undefined for the type DoublyLinkedList<Integer>
System.out.println(list);
}

My method in the Node Class had this method:

public void addHead(E value) {
Node cursor = head;
Node temp = new Node(value);
temp.setPrev(cursor);
temp.setNext(cursor.getNext());
cursor.getNext().setPrev(temp);
cursor.setNext(temp);
length++;
}

From face value it seems the code i copied from an internet site has integer as an argument inside main but the addHead() has an arg declared as (E value) in the method. So i dont understand what was the intent of the person who wrote this?

From my point of view i just want to understand how to fix this and learn about data structures and the errors i am finding.

By the way thank you for sharing a link to your blog. And there was another link. I learned your a very, very intelligent guy. I certainly appreciate being able to read about things you write.

Andrew
Re: Array List problem [message #1022537 is a reply to message #1022508] Fri, 22 March 2013 00:42 Go to previous message
Russell Bateman is currently offline Russell Bateman
Messages: 3136
Registered: July 2009
Location: Provo, Utah, USA
Senior Member

On 03/21/2013 08:42 PM, Andrew Chung wrote:
> Hi Stephane,
> Many thanks for correcting me and replying so quickly. I am new to Java
> and learning about linked lists.
> [snip]

Not to rain on your parade, Andrew, but this forum is for questions
about Eclipse, not Java. You'll be better served in the following forums:

javaranch.com
jguru.com
stackoverflow.com

Thanks for your understanding.
Previous Topic:Can Eclipse 4 have color themes?
Next Topic:Unknown Eclipse Icon in Project Explorer View
Goto Forum:
  


Current Time: Tue Jul 29 21:14:16 EDT 2014

Powered by FUDForum. Page generated in 0.01633 seconds