80 lines
5.3 KiB
HTML
80 lines
5.3 KiB
HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
|
|
<html>
|
|
<head>
|
|
<title>PrintArrayReversedEven.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}
|
|
pre {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace}
|
|
table {color: #888888; background-color: #313335; font-family: monospace}
|
|
.ST4 {color: #9876aa}
|
|
.number {color: #6897bb}
|
|
.string {color: #6a8759}
|
|
.comment {color: #808080}
|
|
.whitespace {color: #505050}
|
|
.ST2 {color: #ffc66d; font-family: monospace; font-style: italic}
|
|
.ST5 {color: #9876aa; font-family: monospace; font-style: italic}
|
|
.ST1 {color: #808080; font-family: monospace; font-weight: bold}
|
|
.ST0 {color: #287bde}
|
|
.literal {color: #cc7832}
|
|
.ST3 {font-family: monospace; font-style: italic}
|
|
-->
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<table width="100%"><tr><td align="center">/home/caleb/ASDV-Java/Exams/Exam2_mvn_Practice_CalebFontenot/src/main/java/com/calebfontenot/exam2_mvn_practice_calebfontenot/PrintArrayReversedEven.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.exam2_mvn_practice_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> PrintArrayReversedEven {
|
|
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST2">main</span>(String[] args)
|
|
{
|
|
<span class="ST3">printRE</span>(<span class="literal">new</span> <span class="literal">int</span>[] {<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>});
|
|
<span class="ST3">printRE</span>(<span class="literal">new</span> <span class="literal">int</span>[<span class="number">2</span>]);
|
|
<span class="ST3">printRE</span>(<span class="literal">new</span> <span class="literal">int</span>[] {<span class="number">2</span>, <span class="number">4</span>, <span class="number">7</span>, <span class="number">8</span>});
|
|
}
|
|
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST2">printRE</span>(<span class="literal">int</span>[] array) {
|
|
<span class="literal">int</span> arrayLength = <span class="number">0</span>;
|
|
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i <= array.<span class="ST4">length</span> - <span class="number">1</span>; i++) {
|
|
<span class="comment">// determine array length.</span>
|
|
<span class="literal">if</span> (array[i] % <span class="number">2</span> == <span class="number">0</span>) {
|
|
arrayLength++;
|
|
}
|
|
}
|
|
<span class="comment">// Now create the return array.</span>
|
|
<span class="literal">int</span>[] returnArray = <span class="literal">new</span> <span class="literal">int</span>[arrayLength];
|
|
<span class="literal">int</span> arrayI = <span class="number">0</span>;
|
|
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i <= array.<span class="ST4">length</span> - <span class="number">1</span>; i++) {
|
|
<span class="literal">if</span> (array[i] % <span class="number">2</span> == <span class="number">0</span>) {
|
|
<span class="comment">// Add even numbers to array.</span>
|
|
returnArray[arrayI] = array[i];
|
|
arrayI++;
|
|
}
|
|
}
|
|
<span class="comment">// Reverse items in the array.</span>
|
|
<span class="literal">int</span>[] reversedReturnArray = <span class="literal">new</span> <span class="literal">int</span>[arrayI];
|
|
<span class="literal">int</span> reverseArrayIterable = arrayI - <span class="number">1</span>;
|
|
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i <= returnArray.<span class="ST4">length</span> - <span class="number">1</span>; i++) {
|
|
reversedReturnArray[reverseArrayIterable] = returnArray[i];
|
|
reverseArrayIterable--;
|
|
}
|
|
<span class="comment">// Now print the array.</span>
|
|
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i <= reversedReturnArray.<span class="ST4">length</span> - <span class="number">1</span>; i++) {
|
|
System.<span class="ST5">out</span>.print(reversedReturnArray[i] + <span class="string">"</span> <span class="string">"</span>);
|
|
}
|
|
System.<span class="ST5">out</span>.println();
|
|
}
|
|
}
|
|
|
|
</pre></body>
|
|
</html>
|