forked from HarryDulaney/intro-to-java-programming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Exercise13_02.java
63 lines (53 loc) · 2.07 KB
/
Exercise13_02.java
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package ch_13;
import java.util.ArrayList;
import java.util.Random;
/**
* 13.2 (Shuffle ArrayList) Write the following method that shuffles an ArrayList of
* numbers:
* public static void shuffle(ArrayList<Number> list)
*/
public class Exercise13_02 {
static ArrayList<Number> testList;
public static void main(String[] args) {
testList = new ArrayList<>();
for (int i = 0; i < 100; i++) {
testList.add(i);
}
System.out.println("Original list of random int Numbers: ");
System.out.println(testList.toString());
System.out.println("Shuffling the list....");
shuffle(testList);
System.out.println("Shuffle completed: ");
System.out.println(testList.toString());
testShuffled(testList);
}
public static void shuffle(ArrayList<Number> list) {
for (int i = 0; i < list.size(); i++) {
int randIdx = new Random().nextInt(99);
Number numberFromCurrIdx = list.get(i);
Number randomRemovedNumber = list.get(randIdx);
//Swap Numbers
list.set(i, randomRemovedNumber);
list.set(randIdx, numberFromCurrIdx);
}
}
static void testShuffled(ArrayList<Number> list) {
System.out.println("Testing shuffled list contains same contents as original list....");
ArrayList<Number> copyOfShuffledList = new ArrayList<>(list);
copyOfShuffledList.sort((o1, o2) -> {
if (o1.intValue() == o2.intValue()) return 0;
return o1.intValue() > o2.intValue() ? 1 : -1;
});
boolean equalSortedArray = true;
int i = 0;
for (; i < 100; i++) {
if (i != copyOfShuffledList.get(i).intValue()) {
equalSortedArray = false;
break;
}
}
if (!equalSortedArray) System.out.println("Shuffled array does not have same contents as original, @see array" +
" index: " + i);
else System.out.println("Shuffled array has same contents as original array: " + equalSortedArray);
}
}