You could implement your own List class. For that you should define a Node, a List class (which will contain nodes) and a Service that will be responsible to create the random numbers.
This service will be represented in a singleton (a class that cannot be instantiated by any other class).
public class MyRandom { private static MyRandom rdn = new MyRandom(); private MyRandom() {} public static MyRandom getInstance() { return rdn; } public int getRandom(int min, int max) { int range = max - min + 1; double rndDouble = Math.random() * range; int rndInt = (int) rndDouble + min; return rndInt; } }
The Node will only contain a value (the random number) and a reference to the next node. This is the Node class
public class MyNode { private final int value; private MyNode next; public MyNode(int value) { this.value = value; next = null; } public void setNext(MyNode next) { this.next = next; } public int getValue() { return value; } public MyNode getNext() { return next; } }
The List class will contain a reference to the root node, which will also be responsible to add new nodes to the list.
Keep in mind that you could use Generics as well.
public final class MyList { private MyNode root; public MyList(int maxNumber) { for (int i = 0; i < maxNumber; i++) { addNode(MyRandom.getInstance().getRandom(0, 99)); } } public boolean isEmpty() { return root == null; } public void addNode(int value) { if (isEmpty()) { root = new MyNode(value); } else { MyNode aux = root; while (aux.getNext() != null) aux = aux.getNext(); aux.setNext(new MyNode(value)); } } public void printList() { if (!isEmpty()) { MyNode aux = root; while (aux.getNext() != null) { System.out.println(aux.getValue()); aux = aux.getNext(); } System.out.println(aux.getValue()); } } }
And the Main must only instantiate the MyList class and call the printList to show the list.
public class Main { public static void main(String[] args) { MyList lista = new MyList(10); lista.printList(); } }
Hope this helps you.