111 lines
7.0 KiB
HTML
111 lines
7.0 KiB
HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
|
|
<html>
|
|
<head>
|
|
<title>ArraysAndMethods.java</title>
|
|
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
|
|
<style type="text/css">
|
|
<!--
|
|
body {color: #000000; background-color: #ffffff; font-family: monospace}
|
|
pre {color: #000000; background-color: #ffffff; font-family: monospace}
|
|
table {color: #404040; background-color: #e9e8e2; font-family: monospace}
|
|
.ST1 {color: #969696; font-family: monospace; font-weight: bold}
|
|
.ST2 {font-family: monospace; font-weight: bold}
|
|
.comment {color: #969696}
|
|
.ST3 {font-family: monospace; font-weight: bold; font-style: italic}
|
|
.ST6 {font-family: monospace; font-style: italic}
|
|
.ST4 {color: #ce54b8; font-family: monospace; font-style: italic}
|
|
.ST0 {color: #287bde}
|
|
.ST5 {color: #ce54b8}
|
|
.string {color: #1e9347}
|
|
.literal {color: #336bdd}
|
|
-->
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<table width="100%"><tr><td align="center">/home/caleb/ASDV-Java/Assignments/lab16_arrays2_CalebFontenot/src/main/java/com/calebfontenot/lab16_arrays2_calebfontenot/ArraysAndMethods.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.lab16_arrays2_calebfontenot;
|
|
|
|
<span class="comment">/**</span>
|
|
<span class="comment"> *</span>
|
|
<span class="comment"> * </span><span class="ST1">@author</span> <span class="comment">caleb</span>
|
|
<span class="comment">*/</span>
|
|
<span class="literal">public</span> <span class="literal">class</span> <span class="ST2">ArraysAndMethods</span> {
|
|
|
|
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST3">printArray</span>(<span class="literal">int</span>[] arrayInput)
|
|
{
|
|
<span class="literal">for</span> (<span class="literal">int</span> i : arrayInput) {
|
|
System.<span class="ST4">out</span>.print(i + <span class="string">"</span> <span class="string">"</span>);
|
|
}
|
|
System.<span class="ST4">out</span>.println();
|
|
}
|
|
|
|
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST3">printArray</span>(<span class="literal">char</span>[] arrayInput)
|
|
{
|
|
<span class="literal">for</span> (<span class="literal">int</span> i : arrayInput) {
|
|
System.<span class="ST4">out</span>.print((<span class="literal">char</span>) i + <span class="string">"</span> <span class="string">"</span>);
|
|
}
|
|
System.<span class="ST4">out</span>.println();
|
|
}
|
|
|
|
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST3">printArray</span>(<span class="literal">double</span>[] arrayInput)
|
|
{
|
|
<span class="literal">for</span> (<span class="literal">double</span> i : arrayInput) {
|
|
System.<span class="ST4">out</span>.print(i + <span class="string">"</span> <span class="string">"</span>);
|
|
}
|
|
System.<span class="ST4">out</span>.println();
|
|
}
|
|
|
|
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">int</span>[] <span class="ST3">reverse</span>(<span class="literal">int</span>[] arrayToReverse)
|
|
{
|
|
<span class="literal">int</span> arrayResult[] = <span class="literal">new</span> <span class="literal">int</span>[arrayToReverse.<span class="ST5">length</span>];
|
|
<span class="literal">int</span> j = (arrayToReverse.<span class="ST5">length</span> - 1);
|
|
<span class="literal">for</span> (<span class="literal">int</span> i = 0; i <= arrayToReverse.<span class="ST5">length</span> - 1; i++) {
|
|
arrayResult[i] = arrayToReverse[j];
|
|
j--;
|
|
}
|
|
<span class="literal">return</span> arrayResult;
|
|
}
|
|
|
|
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">int</span> <span class="ST3">linearSearch</span>(<span class="literal">int</span> list[], <span class="literal">int</span> key)
|
|
{
|
|
<span class="literal">int</span> indexFound = -1;
|
|
<span class="literal">for</span> (<span class="literal">int</span> i = 0; i < list.<span class="ST5">length</span>; ++i) {
|
|
<span class="literal">if</span> (list[i] == key) {
|
|
indexFound = i;
|
|
}
|
|
}
|
|
<span class="literal">return</span> indexFound;
|
|
}
|
|
|
|
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST3">main</span>(String[] args)
|
|
{
|
|
<span class="literal">int</span>[] array = {1, 2, 3, 4, 5};
|
|
<span class="ST6">printArray</span>(array);
|
|
<span class="ST6">printArray</span>(<span class="literal">new</span> <span class="literal">int</span>[]{3, 1, 2, 6, 4});
|
|
<span class="ST6">printArray</span>(<span class="ST6">r</span><span class="ST6">everse</span>(array));
|
|
|
|
System.<span class="ST4">out</span>.println(<span class="ST6">linearSearch</span>(array, 5) != 1
|
|
? <span class="string">"</span><span class="string">found at index </span><span class="string">"</span> + Integer.<span class="ST6">toString</span>(<span class="ST6">l</span><span class="ST6">inearSearch</span>(array, 5))
|
|
: <span class="string">"</span><span class="string">not found at index </span><span class="string">"</span> + Integer.<span class="ST6">toString</span>(<span class="ST6">l</span><span class="ST6">inearSearch</span>(array, 5))
|
|
);
|
|
<span class="literal">double</span>[] doubles = {6.0, 4.4, 1.9, 2.9, 3.4, 3.5};
|
|
<span class="comment">// Use the built-in sort method to sort the double.</span>
|
|
java.util.Arrays.<span class="ST6">sort</span>(doubles);
|
|
|
|
<span class="literal">char</span>[] chars = {<span class="string">'</span><span class="string">a</span><span class="string">'</span>, <span class="string">'</span><span class="string">A</span><span class="string">'</span>, <span class="string">'</span><span class="string">4</span><span class="string">'</span>, <span class="string">'</span><span class="string">F</span><span class="string">'</span>, <span class="string">'</span><span class="string">D</span><span class="string">'</span>, <span class="string">'</span><span class="string">P</span><span class="string">'</span>,};
|
|
<span class="comment">//Sort again</span>
|
|
java.util.Arrays.<span class="ST6">sort</span>(chars);
|
|
<span class="comment">// Now print the sorted arrays.</span>
|
|
<span class="ST6">printArray</span>(doubles);
|
|
<span class="ST6">printArray</span>(chars);
|
|
}
|
|
}
|
|
|
|
</pre></body>
|
|
</html>
|