So, the method that I'm having problems with at the moment is my insertAt method, which takes an integer and an item as parameters and then adds the item to the circular linked list at the index of the list that the integer specifies. If the index passed through is less than 0 or greater than the number of elements in the list then it throws a new Index Out of Bounds Exception. I can't seem to figure out what the problem is with this method. Any advice would be greatly appreciated. The error I'm getting is this:
Exception in thread "main" java.lang.IndexOutOfBoundsException: -1
at clinkedlist.CLinkedList.insertAt(CLinkedList.java: 65)
at clinkedlist.CLinkedListTest.test10(CLinkedListTest .java:114)
at clinkedlist.CLinkedListTest.run(CLinkedListTest.ja va:27)
at clinkedlist.CLinkedListTest.main(CLinkedListTest.j ava:14)
import java.util.*; class Node<T> { T data; Node<T> next; Node(T dat){ data = dat; next = null; } Node(T dat, Node<T> nxt){ data = dat; next = nxt; } } public class CLinkedList<T>{ private Node<T> tail; private int count; //private CLinkedList list; public CLinkedList(){ tail = null; } public void add(T item){ if (tail != null){ Node<T> newNode = new Node<T>(item, tail.next); tail.next = newNode; } else{ tail = new Node<T>(item, null); tail.next = tail; } count++; } public void append(T item){ if (tail != null){ Node<T> newNode = new Node<T>(item, tail.next); tail.next = newNode; tail = newNode; } else{ tail = new Node<T>(item, null); tail.next = tail; } count++; } public void insertAt(int index, T item){ if (index < 0 || index > count){ throw new IndexOutOfBoundsException(Integer.toString(index)); //****This is where the problems start.****// } else if (index == 0){ add(item); } else{ Node<T> node = getNode(index - 1); node.next = new Node<T>(item, node.next); count++; } } public boolean remove(T item){ if (tail != null){ Node<T> remNode = tail; do{ if (remNode.next.data.equals(item)){ if (remNode.next == tail && tail != tail.next){ remNode.next = remNode.next.next; tail = tail.next; } else if (remNode.next == remNode.next.next){ tail = null; } else{ remNode.next = remNode.next.next; } return true; } remNode = remNode.next; }while (remNode != tail); } return false; /*Node<T> temp = new Node<T>(item, tail); if (tail != null){ tail = tail.next; } if (temp != null){ count--; remove(temp.data); return true; } return false;*/ } public T removeAt(int index){ if (index < 0 || index > count){ throw new IndexOutOfBoundsException(Integer.toString(index)); } if (index >= 0){ Node<T> node = getNode(index); remove(node.data); count--; } return null; } public void clear(){ } public void setAt(T item, int index){ if (index < 0 || index >= count ){ throw new IndexOutOfBoundsException(Integer.toString(index)); } Node<T> node = getNode(index); //T result = node.data; node.data = item; } public T getAt(int index){ if (index < 0 || index > count){ throw new IndexOutOfBoundsException(Integer.toString(index)); } Node<T> node = getNode(index); return node.data; } public int indexOf(T item){ Node<T> temp = new Node<T>(item, tail); //Node<T> temp = tail; int i; for (i = 0; !(temp.data).equals(getNode(i)) && temp != null; i++){ temp = temp.next; } if (i == count){ return -1; } return i; } public int size(){ return count; } public boolean isEmpty(){ if (count == 0){ return true; } else{ return false; } } private Node<T> getNode(int index){ Node<T> node = tail; int i; for (i = 0; i < index && node != null; i++){ node = node.next; } return node; } }public class CLinkedListTest { public static void main(String[] args){ CLinkedListTest me = new CLinkedListTest(); me.run(); } public void run(){ test1(); test2(); test3(); test4(); test5(); test6(); test7(); test8(); test9(); test10(); test11(); test12(); test13(); test14(); test15(); test16(); test17(); test18(); test19(); test20(); test21(); } //Test to see that the initial list is empty. public void test1(){ CLinkedList test = new CLinkedList(); assert 0 == test.size(); } //Test to see if append works on an empty CLL. public void test2(){ CLinkedList<String> test = new CLinkedList<String>(); test.append("A"); assert 1 == test.size(); } //Test to see if append is working on a non-empty CLL. public void test3(){ CLinkedList<String> test = new CLinkedList<String>(); test.append("A"); test.append("B"); assert 2 == test.size(); } //Test to see if add is working on an empty CLL. public void test4(){ CLinkedList<String> test = new CLinkedList<String>(); test.add("A"); assert 1 == test.size(); } //Test to see if add is working on a non-empty CLL. public void test5(){ CLinkedList<String> test = new CLinkedList<String>(); test.add("A"); test.add("B"); assert 2 == test.size(); } //Test to see if remove is working on an empty CLL. public void test6(){ CLinkedList<String> test = new CLinkedList<String>(); test.remove("A"); assert 0 == test.size(); assert(test.remove("A") == false); } //Test to see if remove is working on a non-empty CLL. public void test7(){ CLinkedList<String> test = new CLinkedList<String>(); test.add("A"); test.remove("A"); assert 0 == test.size(); assert(test.remove("A") == true); } //Test to see if insertAt is working on an empty CLL. public void test8(){ CLinkedList<String> test = new CLinkedList<String>(); test.insertAt(0, "A"); assert 1 == test.size(); } //Test to see if insertAt is working on a non-empty CLL. public void test9(){ CLinkedList<String> test = new CLinkedList<String>(); test.add("A"); test.add("B"); test.add("C"); test.insertAt(1, "D"); assert 4 == test.size(); } //Test to see if insertAt throws an out OutOfBoundsException. public void test10(){ CLinkedList<String> test = new CLinkedList<String>(); test.insertAt(-1, "A"); assert 0 == test.size(); } //Test to see if removeAt is working on an empty CLL. public void test11(){ CLinkedList<String> test = new CLinkedList<String>(); test.removeAt(0); assert 0 == test.size(); } //Test to see if removeAt is working on a non-empty CLL. public void test12(){ CLinkedList<String> test = new CLinkedList<String>(); test.add("A"); test.add("B"); test.add("C"); test.removeAt(2); assert 2 == test.size(); } //Test to see if removeAt throws an OutOfBoundsException. public void test13(){ CLinkedList<String> test = new CLinkedList<String>(); test.removeAt(-1); assert 0 == test.size(); } //Test to see if setAt overwrites index location in an empty CLL. public void test14(){ CLinkedList<String> test = new CLinkedList<String>(); test.setAt("A", 0); assert 1 == test.size(); } //Test to see if setAt overwrites index location in a non-empty CLL. public void test15(){ CLinkedList<String> test = new CLinkedList<String>(); test.add("A"); test.add("B"); test.add("C"); test.setAt("D", 1); assert 3 == test.size(); } //Test to see if setAt throws an OutOfBoundsException. public void test16(){ CLinkedList<String> test = new CLinkedList<String>(); test.setAt("A", -1); assert 0 == test.size(); } //Test to see if getAt is working on a non-empty CLL. public void test17(){ CLinkedList<String> test = new CLinkedList<String>(); test.add("A"); test.add("B"); test.getAt(1); assert 2 == test.size(); } //Test to see if getAt throws an OutOfBoundsException. public void test18(){ CLinkedList<String> test = new CLinkedList<String>(); test.getAt(-1); assert 0 == test.size(); } //Test to see if indexOf is working in a non-empty CLL. public void test19(){ CLinkedList<String> test = new CLinkedList<String>(); test.add("A"); test.add("B"); test.add("C"); test.indexOf("B"); assert 3 == test.size(); assert(test.indexOf("B") == 1); } //Test to see if isEmpty is working on an empty CLL. public void test20(){ CLinkedList<String> test = new CLinkedList<String>(); test.isEmpty(); assert 0 == test.size(); assert(test.isEmpty() == true); } //Test to see if isEmpty is working on a non-empty CLL. public void test21(){ CLinkedList<String> test = new CLinkedList<String>(); test.add("A"); test.isEmpty(); assert 1 == test.size(); assert(test.isEmpty() == false); } }