Commit ca2d60c1 authored by Dr. Martin Goik's avatar Dr. Martin Goik

Adding null related explanation

parent ade8c121
......@@ -5,50 +5,50 @@ package de.hdm_stuttgart.mi.sd1.task1;
*/
public class HelperArray {
/**
* <p>Filter an array of strings creating a new array containing only those non-<code>null</code> values
* following a given reference string. The original array will be left untouched.</p>
*
* <p>Examples starting from <code>{"brown", "red", "yellow", "green"}</code>:</p>
*
<table class="goikTableDefaults">
* <tr>
* <th>Reference</th>
* <th colspan="2">Result</th>
* </tr>
*
* <tr>
* <td><code>"magenta"</code></td>
* <td><code>{"red", "yellow"}</code></td>
* <td>Both <code>"brown"</code> and <code>"green"</code> precede "magenta" (alphabetically).
* get evicted.</td>
* </tr>
*
* <tr>
* <td><code>"red"</code></td>
* <td><code>{"yellow"}</code></td>
* <td>All values except <code>{"yellow"}</code> precede <code>"red"</code>.</td>
* </tr>
*
* </table>
*
* <section class="implementationHints">
*
* <h3>Implementation hint:</h3>
*
* <ul>
* <li>See {@link String#compareTo(String)}.</li>
* </ul>
*
* </section>
*
* @param values {@link String} or <code>null</code> values remaining untouched.
* @param reference A reference string defining a filter.
* @return A possibly empty new array containing only those non-<code>null</code> values following the
* reference string in alphabetical order. If either argument is null the result will be empty.
*
*/
static public String[] getAlphabeticalFollowers(final String[] values, final String reference) {
/**
* <p>Filter an array of strings for creating a new array containing only those
* non-<code>null</code> values following a given reference string. The arguments array is being untouched.</p>
*
* <p>Examples starting from the series of strings
* <code>{"brown", "red", null, "yellow", "green"}</code>:</p>
*
<table class="goikTableDefaults">
* <tr>
* <th>Reference</th>
* <th colspan="2">Result</th>
* </tr>
*
* <tr>
* <td><code>"magenta"</code></td>
* <td><code>{"red", "yellow"}</code></td>
* <td>Both <code>"brown"</code> and <code>"green"</code> precede "magenta" alphabetically and thus get
* evicted. The <code>null</code> value is being ignored.</td>
* </tr>
*
* <tr>
* <td><code>"red"</code></td>
* <td><code>{"yellow"}</code></td>
* <td>All values except <code>{"yellow"}</code> precede <code>"red"</code>.</td>
* </tr>
*
* </table>
*
* <section class="implementationHints">
*
* <h3>Implementation hint:</h3>
*
* <ul>
* <li>See {@link String#compareTo(String)}.</li>
* </ul>
*
* </section>
*
* @param values {@link String} or <code>null</code> containing {@link String} and/or <code>null</code> values.
* @param reference A reference string defining a filter.
* @return A possibly empty array containing only those non-<code>null</code> values following the
* reference string in alphabetical order. If either argument is <code>null</code> the result will be an empty array.
*/
static public String[] getAlphabeticalFollowers(final String[] values, final String reference) {
return null; // TODO:Implement me correctly
}
......
......@@ -8,10 +8,11 @@ import java.util.Arrays;
public class HelperArray {
/**
* <p>Filter an array of strings creating a new array containing only those non-<code>null</code> values
* following a given reference string. The original array will be left untouched.</p>
* <p>Filter an array of strings for creating a new array containing only those
* non-<code>null</code> values following a given reference string. The arguments array is being untouched.</p>
*
* <p>Examples starting from <code>{"brown", "red", "yellow", "green"}</code>:</p>
* <p>Examples starting from the series of strings
* <code>{"brown", "red", null, "yellow", "green"}</code>:</p>
*
<table class="goikTableDefaults">
* <tr>
......@@ -22,8 +23,8 @@ public class HelperArray {
* <tr>
* <td><code>"magenta"</code></td>
* <td><code>{"red", "yellow"}</code></td>
* <td>Both <code>"brown"</code> and <code>"green"</code> precede "magenta" (alphabetically).
* get evicted.</td>
* <td>Both <code>"brown"</code> and <code>"green"</code> precede "magenta" alphabetically and thus get
* evicted. The <code>null</code> value is being ignored.</td>
* </tr>
*
* <tr>
......@@ -44,31 +45,29 @@ public class HelperArray {
*
* </section>
*
* @param values {@link String} or <code>null</code> values remaining untouched.
* @param values {@link String} or <code>null</code> containing {@link String} and/or <code>null</code> values.
* @param reference A reference string defining a filter.
* @return A possibly empty new array containing only those non-<code>null</code> values following the
* reference string in alphabetical order. If either argument is null the result will be empty.
*
* @return A possibly empty array containing only those non-<code>null</code> values following the
* reference string in alphabetical order. If either argument is <code>null</code> the result will be an empty array.
*/
static public String[] getAlphabeticalFollowers(final String[] values, final String reference) {
if (null == values || null == reference) { // Filter special cases returning an empty
return new String[] {}; // result array.
if (null == values || null == reference) { // Filter special cases returning an empty
return new String[] {}; // result array.
} else {
final String[] resultBuffer = new String[values.length]; // Container for collecting results.
int numFollower = 0; // No follower yet found
final String[] resultBuffer = new String[values.length]; // Container for collecting results.
int numFollower = 0; // No follower found yet
for (final String s: values) { // Search all non-null input[] values for alphabetical
if (null != s && 0 < s.compareTo(reference)) { // followers with respect to reference and append
resultBuffer[numFollower++] = s; // them to resultBuffer[] each time incrementing
} // numFollower.
for (final String s: values) { // Search all non-null input[] values for alphabetical
if (null != s && 0 < s.compareTo(reference)) { // followers with respect to reference and append
resultBuffer[numFollower++] = s; // them to resultBuffer[] each time incrementing
} // numFollower.
}
return Arrays.copyOf( // Create result array containing only
resultBuffer, // alphabetical followers
numFollower);
// Much better: Java stream solution, subject in «Software Development 2»
// Much better: Using Java streams, subject in «Software Development 2»
// return Arrays.stream(values)
// .filter(Objects::nonNull)
......
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