Created
April 14, 2015 11:07
-
-
Save wbars/403aa0f06006ff4cf531 to your computer and use it in GitHub Desktop.
java-obfuscation
This file contains hidden or 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
public class DataStructure { | |
// Create an array | |
private final static int SIZE = 15; | |
private int[] arrayOfInts = new int[SIZE]; | |
public DataStructure() { | |
// fill the array with ascending integer values | |
for (int i = 0; i < SIZE; i++) { | |
arrayOfInts[i] = i; | |
} | |
} | |
public void printEven() { | |
// Print out values of even indices of the array | |
DataStructureIterator iterator = this.new EvenIterator(); | |
while (iterator.hasNext()) { | |
System.out.print(iterator.next() + " "); | |
} | |
System.out.println(); | |
} | |
interface DataStructureIterator extends java.util.Iterator<Integer> { } | |
// Inner class implements the DataStructureIterator interface, | |
// which extends the Iterator<Integer> interface | |
private class EvenIterator implements DataStructureIterator { | |
// Start stepping through the array from the beginning | |
private int nextIndex = 0; | |
public boolean hasNext() { | |
// Check if the current element is the last in the array | |
return (nextIndex <= SIZE - 1); | |
} | |
public Integer next() { | |
// Record a value of an even index of the array | |
Integer retValue = Integer.valueOf(arrayOfInts[nextIndex]); | |
// Get the next even element | |
nextIndex += 2; | |
return retValue; | |
} | |
} | |
public static void main(String s[]) { | |
// Fill the array with integer values and print out only | |
// values of even indices | |
DataStructure ds = new DataStructure(); | |
ds.printEven(); | |
} | |
} |
This file contains hidden or 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
0 2 4 6 8 10 12 14 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment