In: Computer Science
Write a Console Java program that inserts 25 random integers in the range of 0 to 100 into a Linked List.
(Use SecureRandom class from java.security package.
SecureRandom rand = new SecureRandom(); - creates the random number
object
rand.nextInt(100) - generates random integers in the 0 to 100
range)
Using a ListItreator output the contents of the LinkedList in the
reverse order.
Using a ListItreator output the contents of the LinkedList in the original order.
import java.security.SecureRandom;
import java.util.LinkedList;
import java.util.ListIterator;
class Main {
public static void main(String[] args) {
SecureRandom rand = new
SecureRandom(); //creates the random number object
LinkedList<Integer> list = new LinkedList<>(); //
creates the linked list
// loop to add integers
to linked list
for(int i=0; i<25;
i++){
int number = rand.nextInt(100); //generates random integers in the
0 to 100 range
list.add(number);
// adds number to list
}
ListIterator<Integer> itr = list.listIterator(list.size());
// creates ListIterator to iterate list
System.out.println("List
in reverse order:");
// hasPrevious() returns
true if the list has previous element
while
(itr.hasPrevious()) {
System.out.println(itr.previous());
}
System.out.println("\nList in original order:");
// hasNext() returns
true if the list has next element
while (itr.hasNext())
{
System.out.println(itr.next());
}
}
}
Sample output