ASDV-Java/Semester 2/Assignments/MP5_CalebFontenot/Printed HTMLs/ShuffleArrayList.html

102 lines
6.7 KiB
HTML
Raw Normal View History

2023-04-27 12:50:36 -05:00
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>ShuffleArrayList.java</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<style type="text/css">
<!--
body {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace; font-weight: bold}
pre {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace; font-weight: bold}
table {color: #888888; background-color: #313335; font-family: monospace; font-weight: bold}
.literal {color: #cc7832}
.ST3 {font-family: monospace; font-weight: bold; font-style: italic}
.ST0 {color: #287bde}
.number {color: #6897bb}
.string {color: #6a8759}
.comment {color: #808080}
.whitespace {color: #505050}
.ST1 {color: #ffc66d; font-family: monospace; font-weight: bold; font-style: italic}
.ST2 {color: #9876aa; font-family: monospace; font-weight: bold; font-style: italic}
-->
</style>
</head>
<body>
<table width="100%"><tr><td align="center">/home/caleb/ASDV-Java/Semester 2/Assignments/MP5_CalebFontenot/src/main/java/com/calebfontenot/mp5_calebfontenot/ShuffleArrayList.java</td></tr></table>
<pre>
<span class="comment">/*</span>
<span class="comment"> * Click </span><span class="ST0">nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt</span><span class="comment"> to change this license</span>
<span class="comment"> * Click </span><span class="ST0">nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java</span><span class="comment"> to edit this template</span>
<span class="comment"> */</span>
<span class="literal">package</span> com.calebfontenot.mp5_calebfontenot;
<span class="literal">import</span> java.util.*;
<span class="comment">/**</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">@author</span> <span class="comment">caleb</span>
<span class="comment">*/</span>
<span class="literal">public</span> <span class="literal">class</span> ShuffleArrayList {
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST1">shuffle</span>(ArrayList&lt;Number&gt; list) {
<span class="comment">// Create a new Random object.</span>
Random rng = <span class="literal">new</span> Random();
<span class="comment">// Create an ArrayList to store the indices of the elements that have been selected.</span>
ArrayList&lt;Integer&gt; selectedIndices = <span class="literal">new</span> ArrayList&lt;Integer&gt;();
<span class="comment">// Loop through each element in the list.</span>
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i &lt; list.size(); ++i) {
<span class="comment">// Generate a random index that has not been selected before.</span>
<span class="literal">int</span> randomIndex;
<span class="literal">do</span> {
randomIndex = rng.nextInt(list.size()); <span class="comment">// Generate a random integer between 0 (inclusive) and the size of the list (exclusive).</span>
} <span class="literal">while</span> (selectedIndices.contains(randomIndex)); <span class="comment">// Repeat until an unselected index is found.</span>
selectedIndices.add(randomIndex); <span class="comment">// Add the selected index to the list of selected indices.</span>
<span class="comment">//System.out.println(randomIndex + &quot;, &quot; + i);</span>
<span class="comment">// Swap the element at the random index with the element at the current index of the loop.</span>
<span class="comment">// This shuffles the list by randomly selecting an element to swap with the current element at each iteration.</span>
Number temp = list.get(randomIndex); <span class="comment">// Save the element at the random index to a temporary variable.</span>
list.set(randomIndex, list.get(i)); <span class="comment">// Overwrite the element at the random index with the element at the current index of the loop.</span>
list.set(i, temp); <span class="comment">// Set the current index of the loop to the saved element, effectively swapping the two elements.</span>
}
}
<span class="literal">public</span> <span class="literal">static</span> String <span class="ST1">checkForDuplicates</span>(ArrayList&lt;Number&gt; list) {
<span class="comment">// Ensure Array does not include repeat numbers.</span>
<span class="literal">boolean</span> repeatNumber = <span class="literal">false</span>;
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i &lt; list.size(); ++i) {
<span class="literal">for</span> (<span class="literal">int</span> j = <span class="number">0</span>; j &lt; list.size(); ++j) {
<span class="literal">if</span> (i != j) {
<span class="comment">//System.out.println(&quot;Checking &quot; + list.get(i) + &quot; and &quot; + list.get(j));</span>
<span class="literal">if</span> (list.get(i) == list.get(j)) {
repeatNumber = <span class="literal">true</span>;
}
}
}
}
<span class="literal">if</span> (repeatNumber) {
<span class="literal">return</span> <span class="string">&quot;</span><span class="string">Numbers repeat in ArrayList.</span><span class="string">&quot;</span>;
} <span class="literal">else</span> {
<span class="literal">return</span> <span class="string">&quot;</span><span class="string">Numbers do not repeat in ArrayList.</span><span class="string">&quot;</span>;
}
}
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST1">main</span>(String[] args) {
<span class="literal">final</span> <span class="literal">int</span> ARRAY_SIZE = <span class="number">50</span>;
ArrayList&lt;Number&gt; list = <span class="literal">new</span> ArrayList&lt;&gt;();
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i &lt; ARRAY_SIZE; ++i) {
list.add(i + <span class="number">1</span>); <span class="comment">// Fill ArrayList with sequential numbers.</span>
}
System.<span class="ST2">out</span>.println(list);
System.<span class="ST2">out</span>.println(<span class="ST3">c</span><span class="ST3">heckForDuplicates</span>(list));
<span class="ST3">shuffle</span>(list);
System.<span class="ST2">out</span>.println(list);
System.<span class="ST2">out</span>.println(<span class="ST3">c</span><span class="ST3">heckForDuplicates</span>(list));
}
}
</pre></body>
</html>