Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.