Results 1 to 2 of 2

Thread: <Identifier> Expected Error Circular Linked List Java

  1. #1
    Join Date
    Mar 2010
    Posts
    1

    <Identifier> Expected Error Circular Linked List Java

    I know there are other threads that discuss this error, but none of them seem to be my problem. I'm getting the <Identifier> expected error on the line CircularList.CircularListIterator(int index){ . It is the constructor for CircularListIterator which is a nested class inside CircularList(must be written as a nested class). Does anyone know what the problem is?

    The whole code is:

    import java.lang.*;
    import java.util.*;
    public class CircularList<E> extends AbstractSequentialList<E>{

    private CircularNode<E> firstNode;
    private CircularNode<E> lastNode;
    private int size;

    CircularList(){
    CircularList<E> list = new CircularList<E>();
    firstNode = null;
    lastNode = null;
    size = 0;
    }

    public int size(){
    return this.size;
    }

    public E get(int index){
    int tempIndex = 0;
    CircularNode<E> temp = new CircularNode<E>();
    temp = firstNode;
    if(index < 0 || index >= size()){
    throw new IndexOutOfBoundsException();
    }
    else{
    while (tempIndex != index){
    temp = temp.getNextNode();
    tempIndex++;
    }
    }
    return temp.getDataElement();

    }

    public CircularNode<E> getNode(int index){
    int tempIndex = 0;
    CircularNode<E> temp = new CircularNode<E>();
    temp = firstNode;
    if(index < 0 || index >= size()){
    throw new IndexOutOfBoundsException();
    }
    else{
    while(tempIndex != index){
    temp = temp.getNextNode();
    tempIndex++;
    }
    }
    return temp;
    }

    public E remove(int index){
    int tempIndex = 0;
    CircularNode<E> prev = new CircularNode<E>();
    CircularNode<E> next = new CircularNode<E>();
    CircularNode<E> temp = new CircularNode<E>();
    temp = firstNode;
    if(index < 0 || index > size-1){
    throw new IndexOutOfBoundsException();
    }
    else{
    if(index == 0){
    firstNode = firstNode.getNextNode();
    size--;
    }
    else if(index == size-1){
    lastNode = lastNode.getPreviousNode();
    size--;
    }
    else if(firstNode == lastNode){
    firstNode = null;
    lastNode = null;
    size--;
    }
    else{
    while(tempIndex != index){
    temp = temp.getNextNode();
    tempIndex++;
    }
    prev = temp.getPreviousNode();
    next = temp.getNextNode();
    prev.setNextNode(next);
    next.setPreviousNode(prev);
    size--;
    }
    }
    return temp.getDataElement();
    }

    public void add(int index, E element){
    int tempIndex = 0;
    CircularNode<E> newNode;
    CircularNode<E> temp = new CircularNode<E>();
    CircularNode<E> next = new CircularNode<E>();
    CircularNode<E> prev = new CircularNode<E>();
    if(index < 0 || index > size-1){
    throw new IndexOutOfBoundsException();
    }
    temp = firstNode;
    while(tempIndex != index){
    temp = temp.getNextNode();
    tempIndex++;
    }
    if(size == 0){
    newNode = new CircularNode<E>(element, firstNode, lastNode);
    firstNode.setNextNode(newNode);
    lastNode.setPreviousNode(newNode);
    size++;
    }
    else if(index == 0 && size != 0){
    newNode = new CircularNode<E>(element, firstNode, temp);
    firstNode.setNextNode(newNode);
    temp.setPreviousNode(newNode);
    size++;
    }
    else if(index == size-1 && size != 0){
    newNode = new CircularNode<E>(element, temp, lastNode);
    lastNode.setPreviousNode(newNode);
    temp.setNextNode(newNode);
    size++;
    }
    else{
    newNode = new CircularNode<E>(element, temp.getPreviousNode(), temp);
    (temp.getPreviousNode()).setNextNode(newNode);
    temp.setPreviousNode(newNode);
    size++;
    }
    }

    public E set(int index, E element){
    int tempIndex = 0;
    CircularNode<E> temp = new CircularNode<E>();
    temp = firstNode;
    if(index < 0 || index > size-1){
    throw new IndexOutOfBoundsException();
    }
    while(tempIndex != index){
    temp = temp.getNextNode();
    tempIndex++;
    }
    E tempElement = temp.getDataElement();
    temp.setDataElement(element);
    return tempElement;
    }

    public CircularList.CircularListIterator<E> iterator(){
    CircularList.CircularListIterator(0);
    }

    public CircularListIterator<E> listIterator(int index){
    int tempIndex = index;
    CircularList.CircularListIterator(tempIndex);
    }

    public class CircularListIterator<E> implements ListIterator<E>{

    private int nextIndex;
    private CircularNode<E> nextNode;
    private CircularNode<E> previousNode;

    CircularList.CircularListIterator(int index){
    tempIndex = 0;
    CircularNode<E> temp = new CircularNode<E>();
    temp = firstNode;
    if(index < 0 || index > size-1){
    throw new IndexOutOfBoundsException();
    }
    while(tempIndex != index){
    temp = temp.getNextNode();
    tempIndex++;
    }
    while(tempIndex <= size-1){
    System.out.println(temp);
    temp = temp.getNextNode();
    tempIndex++;
    }
    }

    public boolean hasNext(){
    return true;
    }

    public boolean hasPrevious(){
    return true;
    }
    }
    }

    Thank you for your help

  2. #2
    Join Date
    Nov 2009
    Posts
    518

    Re: <Identifier> Expected Error Circular Linked List Java

    Hello,
    You are saying that you are getting the error in this line
    Code:
    CircularList.CircularListIterator(int index){
    Just try using the CircularListIterator(int index), that is do no use the inner class in this case. It is difficult to say the exact error behind your code, as only one error is not sufficient for understanding the reason behind it. If you use the JUnit software for testing the code, then you can just test the line which you are getting the error and this can solve your problem.

Similar Threads

  1. <identifier> expected error
    By mbueling in forum Software Development
    Replies: 2
    Last Post: 18-10-2011, 06:04 AM
  2. Problem using Linked list in java
    By Aaliya Seth in forum Software Development
    Replies: 5
    Last Post: 18-02-2010, 01:06 AM
  3. "Identifier Expected" error in Dr Java
    By adam.taylor in forum Software Development
    Replies: 5
    Last Post: 12-05-2009, 11:55 AM
  4. Identifier Expected Error in JAVA
    By Brake Fail in forum Software Development
    Replies: 7
    Last Post: 25-10-2008, 02:35 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,294,018.99309 seconds with 17 queries