Skip to content

Instantly share code, notes, and snippets.

@carousel
Last active December 17, 2019 13:58
Show Gist options
  • Save carousel/adc1f12fe3233596959e04c2b0681a71 to your computer and use it in GitHub Desktop.
Save carousel/adc1f12fe3233596959e04c2b0681a71 to your computer and use it in GitHub Desktop.
Caclulate new size for dynamic array in Java
private Integer newSize(Integer currentSize, Integer inputSize) {
if (currentSize > inputSize) {
return currentSize * 2;
} else if (currentSize < inputSize) {
return (inputSize % currentSize == 0) ? (currentSize + inputSize) : (currentSize + inputSize + 1);
} else {
return currentSize + inputSize;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment