at the moment, I'm exercising with linked list but I have a problem with a code. The code below runs and work, but when I'm trying to add some nodes using a for generating random numbers it gave me this error. Before adding the for the code works and run adding the now as you can see into main. Maybe I missed something. Can someone help me to understand?
P.S. The commented part was the part of the main that I tryed to "upgrade".
import java.util.Random;
class Node {
private int value;
private Node next = null;
public Node(int value) {
this.value = value;
}
public int getValue() { return this.value; }
public Node getNext() { return this.next; }
public void setNext(Node pNext) { this.next = pNext; }
}
public class linked {
private Node head;
private Node tail;
private int size;
public int getSize() { return this.size; }
public void insert (Node ele) {
if (this.head == null) {
this.tail = ele;
this.head = this.tail;
}
else {
this.tail.setNext(ele);
this.tail = ele;
}
this.size++;
}
@Override
public String toString() {
StringBuilder ret = null;
if ((this.head != null) && (this.tail != null)) {
ret = new StringBuilder("[Dimensione: " + this.size
+ ", Head: "
+ this.head.getValue()
+ ", Tail: "
+ this.tail.getValue()
+ "] Elementi: ");
Node tmp = this.head;
while (tmp != null) {
ret.append(tmp.getValue() + " -> ");
tmp = tmp.getNext();
}
ret.append("/");
}
return ret == null ? "[null]" : ret.toString();
}
public static void main (String args[])
{
linked ll = new linked();
System.out.println(ll);
for(int i=0; i<15; i++) {
Random rand = new Random();
double pazz = rand.nextInt(50) + 1;
ll.insert(new Node(pazz));
}
/*
ll.insert(new Node(10));
System.out.println(ll);
ll.insert(new Node(25));
System.out.println(ll);
ll.insert(new Node(12));
System.out.println(ll);
ll.insert(new Node(20));
System.out.println(ll);
*/
}
}