Answer:
- import java.util.ArrayList;
- import java.util.Random;
-
- public class Main {
-
- public static void main(String[] args) {
- ArrayList<String> arrStr = new ArrayList<String>(10);
- arrStr.add("Apple");
- arrStr.add("Banana");
- arrStr.add("Cat");
- arrStr.add("Dog");
- arrStr.add("Elephant");
- arrStr.add("Fox");
- arrStr.add("Good");
- arrStr.add("Home");
- arrStr.add("Icecream");
- arrStr.add("Jaspel");
-
- Random rand = new Random();
- int n1 = rand.nextInt(10);
- int n2 = rand.nextInt(10);
- while(n1 == n2){
- n2 = rand.nextInt(10);
- }
-
- System.out.println(arrStr.get(n1));
- System.out.println(arrStr.get(n2));
- }
- }
Step-by-step explanation:
The solution code is written in Java.
Firstly, create an array list and set 10 strings to the array list (Line 7-17).
Use random nextInt method to generate a number between 0-9 as index (Line 19-21).
While the first random number and the second random number is equal, find a new random random (Line 22-24).
At last print out the first random string and second random string using the n1 & n2 as index to extract the string items from the array list (Line 26-27).