|
Re: Array List problem [message #1022204 is a reply to message #1021992] |
Thu, 21 March 2013 13:35 |
|
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 13:35] Report message to a moderator
|
|
|
|
Re: Array List problem [message #1022537 is a reply to message #1022508] |
Fri, 22 March 2013 04:42 |
|
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.
|
|
|
Powered by
FUDForum. Page generated in 0.05688 seconds