/home/caleb/ASDV-Java/Semester 2/Assignments/MP5_CalebFontenot/src/main/java/com/calebfontenot/mp5_calebfontenot/ShuffleArrayList.java |
nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt
nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java
package com.calebfontenot.mp5_calebfontenot;
import java.util.*;
public class ShuffleArrayList {
public static void shuffle(ArrayList<Number> list) {
Random rng = new Random();
ArrayList<Integer> selectedIndices = new ArrayList<Integer>();
for (int i = 0; i < list.size(); ++i) {
int randomIndex;
do {
randomIndex = rng.nextInt(list.size());
} while (selectedIndices.contains(randomIndex));
selectedIndices.add(randomIndex);
Number temp = list.get(randomIndex);
list.set(randomIndex, list.get(i));
list.set(i, temp);
}
}
public static String checkForDuplicates(ArrayList<Number> list) {
boolean repeatNumber = false;
for (int i = 0; i < list.size(); ++i) {
for (int j = 0; j < list.size(); ++j) {
if (i != j) {
if (list.get(i) == list.get(j)) {
repeatNumber = true;
}
}
}
}
if (repeatNumber) {
return "Numbers repeat in ArrayList.";
} else {
return "Numbers do not repeat in ArrayList.";
}
}
public static void main(String[] args) {
final int ARRAY_SIZE = 50;
ArrayList<Number> list = new ArrayList<>();
for (int i = 0; i < ARRAY_SIZE; ++i) {
list.add(i + 1);
}
System.out.println(list);
System.out.println(checkForDuplicates(list));
shuffle(list);
System.out.println(list);
System.out.println(checkForDuplicates(list));
}
}