Commit 38bb9053 authored by Dominikus Herzberg's avatar Dominikus Herzberg
Browse files

HTML neu generiert für Stakk-Aufgabe

parent 1f959579
...@@ -646,13 +646,13 @@ summary:after { ...@@ -646,13 +646,13 @@ summary:after {
</table> </table>
</div> </div>
<div class="paragraph"> <div class="paragraph">
<p>Für ihre Klasse <code>Stack</code> bzw. <code>Stapel</code> aus den vorherigen Übungsblättern ist die Situation sogar noch etwas einfacher. Schaffen Sie es analog zu dem Beispiel der <code>ArrayList</code> auch einen generischen <code>Stack</code> zu bauen, so dass z.B. die folgenden Aufrufe möglich werden?</p> <p>Für ihre Klasse <code>Stakk</code> bzw. <code>Stapel</code> aus den vorherigen Übungsblättern ist die Situation sogar noch etwas einfacher. Schaffen Sie es analog zu dem Beispiel der <code>ArrayList</code> auch einen generischen <code>Stakk</code> zu bauen, so dass z.B. die folgenden Aufrufe möglich werden?</p>
</div> </div>
<div class="listingblock"> <div class="listingblock">
<div class="content"> <div class="content">
<pre>Stack&lt;String&gt; stringStack = new Stack&lt;String&gt;("First"); <pre>Stakk&lt;String&gt; stringStack = new Stakk&lt;String&gt;("First");
stringStack.push("Second"); stringStack.push("Second");
Stack&lt;Double&gt; doubleStack = new Stack&lt;Double&gt;(Double.NaN); Stakk&lt;Double&gt; doubleStack = new Stakk&lt;Double&gt;(Double.NaN);
doubleStack.push(3.5); doubleStack.push(3.5);
double d = doubleStack.pop();</pre> double d = doubleStack.pop();</pre>
</div> </div>
...@@ -661,14 +661,14 @@ double d = doubleStack.pop();</pre> ...@@ -661,14 +661,14 @@ double d = doubleStack.pop();</pre>
<summary>Lösung</summary> <summary>Lösung</summary>
<div class="listingblock"> <div class="listingblock">
<div class="content"> <div class="content">
<pre class="CodeRay highlight"><code data-lang="java"><span class="type">class</span> <span class="class">Stack</span>&lt;E&gt; { <pre class="CodeRay highlight"><code data-lang="java"><span class="type">class</span> <span class="class">Stakk</span>&lt;E&gt; {
E value; E value;
<span class="predefined-type">Stack</span>&lt;E&gt; rest; Stakk&lt;E&gt; rest;
<span class="predefined-type">Stack</span>(E value, <span class="predefined-type">Stack</span>&lt;E&gt; rest) { Stakk(E value, Stakk&lt;E&gt; rest) {
<span class="local-variable">this</span>.value = value; <span class="local-variable">this</span>.value = value;
<span class="local-variable">this</span>.rest = rest; <span class="local-variable">this</span>.rest = rest;
} }
<span class="predefined-type">Stack</span>(E value) { Stakk(E value) {
<span class="local-variable">this</span>(value, <span class="predefined-constant">null</span>); <span class="local-variable">this</span>(value, <span class="predefined-constant">null</span>);
} }
<span class="directive">public</span> <span class="predefined-type">String</span> toString() { <span class="directive">public</span> <span class="predefined-type">String</span> toString() {
...@@ -683,7 +683,7 @@ double d = doubleStack.pop();</pre> ...@@ -683,7 +683,7 @@ double d = doubleStack.pop();</pre>
<span class="keyword">return</span> <span class="integer">1</span> + rest.size(); <span class="keyword">return</span> <span class="integer">1</span> + rest.size();
} }
<span class="type">void</span> push(E val) { <span class="type">void</span> push(E val) {
rest = <span class="keyword">new</span> <span class="predefined-type">Stack</span>&lt;E&gt;(value, rest); rest = <span class="keyword">new</span> Stakk&lt;E&gt;(value, rest);
value = val; value = val;
} }
E pop() { E pop() {
...@@ -695,17 +695,17 @@ double d = doubleStack.pop();</pre> ...@@ -695,17 +695,17 @@ double d = doubleStack.pop();</pre>
E<span class="type">[]</span> toArray(E<span class="type">[]</span> base) { <i class="conum" data-value="1"></i><b>(1)</b> E<span class="type">[]</span> toArray(E<span class="type">[]</span> base) { <i class="conum" data-value="1"></i><b>(1)</b>
E<span class="type">[]</span> ar = <span class="predefined-type">Arrays</span>.copyOf(base, size()); E<span class="type">[]</span> ar = <span class="predefined-type">Arrays</span>.copyOf(base, size());
<span class="type">int</span> i = <span class="integer">0</span>; <span class="type">int</span> i = <span class="integer">0</span>;
<span class="predefined-type">Stack</span>&lt;E&gt; cur; Stakk&lt;E&gt; cur;
<span class="keyword">for</span>(cur = <span class="local-variable">this</span>; cur.rest != <span class="predefined-constant">null</span> ; cur = cur.rest) { <span class="keyword">for</span>(cur = <span class="local-variable">this</span>; cur.rest != <span class="predefined-constant">null</span> ; cur = cur.rest) {
ar[i++] = cur.value; ar[i++] = cur.value;
} }
ar[i] = cur.value; ar[i] = cur.value;
<span class="keyword">return</span> ar; <span class="keyword">return</span> ar;
} }
<span class="directive">static</span> &lt;E&gt; <span class="predefined-type">Stack</span> fromArray(E<span class="type">[]</span> ar) { <i class="conum" data-value="2"></i><b>(2)</b> <span class="directive">static</span> &lt;E&gt; Stakk fromArray(E<span class="type">[]</span> ar) { <i class="conum" data-value="2"></i><b>(2)</b>
<span class="predefined-type">Stack</span>&lt;E&gt; s = <span class="predefined-constant">null</span>; Stakk&lt;E&gt; s = <span class="predefined-constant">null</span>;
<span class="keyword">for</span>(<span class="type">int</span> i = <span class="integer">0</span>; i &lt; ar.length; i++) { <span class="keyword">for</span>(<span class="type">int</span> i = ar.length-<span class="integer">1</span>; i &gt;= <span class="integer">0</span>; i--) {
s = <span class="keyword">new</span> <span class="predefined-type">Stack</span>&lt;E&gt;(ar[i], s); s = <span class="keyword">new</span> Stakk&lt;E&gt;(ar[i], s);
} }
<span class="keyword">return</span> s; <span class="keyword">return</span> s;
} }
...@@ -720,7 +720,7 @@ double d = doubleStack.pop();</pre> ...@@ -720,7 +720,7 @@ double d = doubleStack.pop();</pre>
</tr> </tr>
<tr> <tr>
<td><i class="conum" data-value="2"></i><b>2</b></td> <td><i class="conum" data-value="2"></i><b>2</b></td>
<td>Auch Methoden können eigene Typparameter besitzen. Bei dieser statischen Methode ist das nötig, da bei dem Aufruf von <code>Stack.fromArray</code> kein Objekt vom Typ <code>Stack</code> beteiligt ist von dem man den Typparameter ableiten könnte.</td> <td>Auch Methoden können eigene Typparameter besitzen. Bei dieser statischen Methode ist das nötig, da bei dem Aufruf von <code>Stakk.fromArray</code> kein Objekt vom Typ <code>Stakk</code> beteiligt ist von dem man den Typparameter ableiten könnte.</td>
</tr> </tr>
</table> </table>
</div> </div>
...@@ -832,7 +832,7 @@ m.state = <span class="string"><span class="delimiter">&quot;</span><span class= ...@@ -832,7 +832,7 @@ m.state = <span class="string"><span class="delimiter">&quot;</span><span class=
</div> </div>
<div id="footer"> <div id="footer">
<div id="footer-text"> <div id="footer-text">
Last updated 2017-12-04 14:47:15 Mitteleuropõische Zeit Last updated 2018-01-31 09:48:48 Mitteleuropõische Zeit
</div> </div>
</div> </div>
</body> </body>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment