-
Notifications
You must be signed in to change notification settings - Fork 74
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added Simple 2D Iterator Solution in java #347
Conversation
} | ||
|
||
public boolean hasNext() { | ||
return row < array.length && col < array[row].length; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if you actually need to check the column here. Your next()
function seems like it would prevent col
from ever being >= the number of columns in your input array.
} | ||
|
||
public class Codingis4noobs2 { | ||
public static void main(String[] args) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nothing "wrong" with demonstrating your solution this way, but I'm going to add a comment to the PR demonstrating how to incorporate and test your code with JUnit. It'll probably look a bit wonky since it'll be using JUnit directly (instead of through an IDE or using a package manager), but it's worth taking a look at. I'm not going to ask that you change your PR to use JUnit, just take a look.
This solution works! If you wanted to use JUnit tests, we support that in the repo by leveraging the JUnit jar directly. It's included in // 2D Array Itreator
// Testing Imports
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import java.util.Iterator;
import java.util.NoSuchElementException;
class NestedIterator<T> implements Iterator<T> {
// clipped for brevity
}
public class Codingis4noobs2 {
public static void main(String[] args) {
Integer[][] array = new Integer[][] {
{ 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 }
};
NestedIterator<Integer> iterator = new NestedIterator<Integer>(array);
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
}
// Test case one
@Test
@DisplayName("Should return 1-9, in order")
public void testCaseOne() {
Integer[][] array = new Integer[][] {
{ 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 }
};
NestedIterator<Integer> iterator = new NestedIterator<Integer>(array);
assertEquals(iterator.next(), 1);
assertEquals(iterator.next(), 2);
assertEquals(iterator.next(), 3);
assertEquals(iterator.hasNext(), true);
assertEquals(iterator.next(), 4);
assertEquals(iterator.next(), 5);
assertEquals(iterator.next(), 6);
assertEquals(iterator.hasNext(), true);
assertEquals(iterator.next(), 7);
assertEquals(iterator.next(), 8);
assertEquals(iterator.next(), 9);
assertEquals(iterator.hasNext(), false);
}
} You can run the tests by copying #!/bin/bash
# Note: This script will execute as if in the path from which it is called
junit_jar="../../../../../resources/java/junit5-1.8.0-M1.jar"
# Runs the JUnit5 tests
run_test() {
# Get a list of all files in the current directory that end in `.java`
java_files=$(ls | grep java)
# Compile the source and test files for this challenge, put the *.class files
# in the 'out/' folder. Include the JUnit standalone .jar in the classpath.
javac -d out -cp $junit_jar $java_files
# Run the standalone JUnit testrunner with desired options, on the *.class files
# in the 'out/' folder.
java -jar $junit_jar \
--classpath out \
--scan-class-path \
--details tree \
--include-classname '.*'
}
# clipped for brevity If you're wondering, this is exactly how I went about testing your solution to make sure it worked. 😁 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I second what @ericwburden said. Take a look at the JUnit implementation he added to the PR.
Language
Java
Challenge
Simple 2D Iterator
Closes #181