Skip to content

Instantly share code, notes, and snippets.

@peter-tackage
Created March 27, 2014 20:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peter-tackage/9818470 to your computer and use it in GitHub Desktop.
Save peter-tackage/9818470 to your computer and use it in GitHub Desktop.
Integer array incrementer
public class Incrementer {
public static int[] increment(int[] input) {
int index = input.length -1;
while(++input[index] % 10 == 0) {
input[index] = input[index] % 10;
if(index == 0) {
int[] result = new int[input.length + 1];
result[0] = 1;
return result;
}
index--;
}
return input;
}
}
public class IncrementerTest extends TestCase {
public void test_incrementSingleFromZero() {
assertEquals(new int[] { 1 }, Incrementer.increment(new int[] { 0 }));
}
public void test_incrementSingleFromNine() {
assertEquals(new int[] { 1, 0 }, Incrementer.increment(new int[] { 9 }));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment