-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandomGeneratorDemo.java
More file actions
executable file
·45 lines (41 loc) · 1.2 KB
/
RandomGeneratorDemo.java
File metadata and controls
executable file
·45 lines (41 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import java.io.*;
import java.util.*;
class RandomGenerator<T>{
protected void randomize(List<T> arr){
int len = arr.size();
Random rand = new Random();
for (int i = len -1;i>0;i--){
int index = rand.nextInt(i);
T temp = (T) arr.get(index);
arr.set(index,arr.get(i));
arr.set(i , (T) temp);
}
}
}
public class RandomGeneratorDemo<T>{
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
// Console reader = System.console();
if (reader == null) {
System.out.println("No console: non-interactive mode!");
System.exit(0);
}
List<String> arr = new ArrayList<String>();
System.out.print("Enter number of elements of array: ");
try {
int entries = Integer.parseInt(reader.readLine().trim());
for (int i = 0; i < entries;++i) {
System.out.print("Enter value : ");
String value = reader.readLine().trim();
arr.add(value);
}
} catch (NumberFormatException e) {
System.out.println("invalid input!..retry please !!");
} finally {
reader.close();
}
RandomGenerator<String> random1 = new RandomGenerator<>();
random1.randomize(arr);
System.out.println(arr);
}
}