G
Guest
Gast
bin gerade dabei, eine klasse zu schreiben, die eine verkettete lineare liste von objekten darstellt. das interface habe ich vorgegeben:
jetzt hab ich nur das problem, dass mir beim testen:
333
---------
Exception in thread "main" java.lang.NullPointerException
at LinkedList.add(LinkedList.java:31)
at Test.TestAdd(test.java:11)
at Test.main(Test.java:83)
ausgegeben wird...
Code:
public interface List {
public void addFirst(Object o);
public void add(Object o);
public void clear();
public boolean contains(Object o);
public Object getFirst();
public boolean isEmpty();
public int size();
public Object[] toArray();
public boolean remove(Object o);
}
jetzt hab ich nur das problem, dass mir beim testen:
333
---------
Exception in thread "main" java.lang.NullPointerException
at LinkedList.add(LinkedList.java:31)
at Test.TestAdd(test.java:11)
at Test.main(Test.java:83)
ausgegeben wird...
Code:
public class LinkedList implements List {
protected class ListNode {
protected Object value = null;
protected ListNode next = null;
}
protected ListNode head = null;
// Methoden ...
public void addFirst(Object o){
ListNode newNode = new ListNode();
newNode.value = o;
newNode.next = head;
head = newNode;
}
public void add(Object o){
ListNode newNode = new ListNode();
newNode.value = o;
newNode.next = null;
ListNode node = head;
while (node.next != null) {
node = node.next;
}
node.next = newNode;
}
public void clear(){
head = null;
}
public boolean contains(Object o){
ListNode node = head;
while (node != null) {
if (node.value.equals(o)) return true;
node = node.next;
}
return false; // Ende der Liste erreicht, nicht gefunden
}
public Object getFirst(){
return head;
}
public boolean isEmpty(){
return head == null;
}
public int size(){
int length = 0;
ListNode node = head;
while (node != null) {
node = node.next;
length = length + 1;
}
return length;
}
public Object[] toArray(){
Object[] feld = new Object[size()];
ListNode node = head;
for (int i = 0; i < feld.length; i++) {
feld[i] = node.value;
node=node.next;
}
return feld;
}
public boolean remove(Object o){
ListNode node = head;
if (node.value.equals(o)){
head = node.next;
}
else{
while (node != null) {
if (node.next.value.equals(o)){
node.next = node.next.next;
return true;
}
node = node.next;
}
return false;
}
return false;
}
public void Print(){
ListNode node = head;
while (node != null) {
System.out.println(node.value);
node = node.next;
}
}
public void PrintElement(Object o){
ListNode node = head;
System.out.println(node.value);
}
public String toString() {
StringBuilder sb = new StringBuilder("Liste: < ");
for (ListNode node = head; node != null; node = node.next) {
sb.append(node.value).append(" ");
}
sb.append(">");
return sb.toString();
}
}
Code:
public class Test {
public static void TestaddFirst(){
LinkedList test = new LinkedList();
test.addFirst("333");
test.Print();
}
public static void TestAdd(){
LinkedList test = new LinkedList();
test.add("777");
test.add("222");
test.add("111");
test.Print();
}
public static void Testclear(){
LinkedList test = new LinkedList();
test.addFirst("777");
test.add("222");
test.add("111");
test.clear();
test.Print();
}
public static void Testcontains(){
LinkedList test = new LinkedList();
test.addFirst("777");
test.add("222");
test.add("111");
test.add("888");
test.add("666");
test.add("444");
if (test.contains("222"))
System.out.println("enthaelt '222'");
else
System.out.println("enthaelt '222' nicht");
if (test.contains("555"))
System.out.println("enthaelt '555'");
else
System.out.println("enthaelt '555' nicht");
}
public static void TestgetFirst(){
LinkedList test = new LinkedList();
test.addFirst("777");
test.add("222");
test.add("111");
test.add("888");
test.PrintElement(test.getFirst());
}
public static void TestisEmpty(){
LinkedList test = new LinkedList();
test.addFirst("777");
test.add("222");
test.add("111");
test.add("888");
if (test.isEmpty())
System.out.println("leer");
else
System.out.println("nicht leer");
}
public static void Testsize(){
LinkedList test = new LinkedList();
test.addFirst("777");
test.add("222");
test.add("111");
System.out.println(test.size());
}
public static void TestRemove(){
LinkedList test = new LinkedList();
test.addFirst("777");
test.add("222");
test.add("111");
test.Print();
test.remove("111");
System.out.println("after remove");
test.Print();
}
public static void main(String[] args) {
TestaddFirst();
System.out.println("--------");
TestAdd();
System.out.println("--------");
Testclear();
System.out.println("--------");
Testcontains();
System.out.println("--------");
TestgetFirst();
System.out.println("--------");
TestisEmpty();
System.out.println("--------");
Testsize();
System.out.println("--------");
}
}