-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| package oving5.debugging; | ||
|
|
||
| import java.util.Iterator; | ||
| import java.util.NoSuchElementException; | ||
|
|
||
| public class StringMergingIterator implements Iterator<String> { | ||
|
|
||
| private final Iterator<String> first; | ||
| private final Iterator<String> second; | ||
| private boolean turnSwitch = true; | ||
|
|
||
| public StringMergingIterator(Iterator<String> first, Iterator<String> second) { | ||
| this.first = first; | ||
| this.second = second; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean hasNext() { | ||
| return this.first.hasNext() || this.second.hasNext(); | ||
| } | ||
|
|
||
| @Override | ||
| public String next() { | ||
| if (!this.hasNext()) { | ||
| throw new NoSuchElementException(); | ||
| } | ||
|
|
||
| String result = null; | ||
|
|
||
| if (!this.first.hasNext()) { | ||
| result = this.first.next(); | ||
| } else if (!this.second.hasNext()) { | ||
| result = this.second.next(); | ||
| } else { | ||
| if (this.turnSwitch) { | ||
| result = this.first.next(); | ||
| this.turnSwitch = false; | ||
| } | ||
| if (!this.turnSwitch) { | ||
| result = this.second.next(); | ||
| this.turnSwitch = true; | ||
| } | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
| } |
40 changes: 40 additions & 0 deletions
40
src/main/java/oving5/debugging/StringMergingIteratorProgram.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| package oving5.debugging; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Iterator; | ||
| import java.util.List; | ||
|
|
||
| public class StringMergingIteratorProgram { | ||
|
|
||
| public static void main(String[] args) throws Exception { | ||
| Iterator<String> one = List.of("a", "b").iterator(); | ||
| Iterator<String> two = List.of("c", "d", "e").iterator(); | ||
|
|
||
| StringMergingIterator mergeIterator = new StringMergingIterator(one, two); | ||
|
|
||
| List<String> values = new ArrayList<>(); | ||
|
|
||
| while (mergeIterator.hasNext()) { | ||
| values.add(mergeIterator.next()); | ||
| } | ||
|
|
||
| List<String> expectedOutput = List.of("a", "c", "b", "d", "e"); | ||
|
|
||
| if (values.size() != expectedOutput.size()) { | ||
| throw new Exception( | ||
| "The merged output did not contain the expected number of values. Try using " | ||
| + "the VS Code debugger to see the difference between the lists"); | ||
| } | ||
|
|
||
| for (int i = 0; i < expectedOutput.size(); i++) { | ||
| if (!values.get(i).equals(expectedOutput.get(i))) { | ||
| throw new Exception( | ||
| "The iterator did not correctly merge the output. Try using the VS Code " | ||
| + "debugger to see the difference between the lists"); | ||
| } | ||
| } | ||
|
|
||
| System.out.println( | ||
| "Success! StringMergingIterator correctly merged the output of the two lists"); | ||
| } | ||
| } |