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
Bookmarks