Skip to content

Instantly share code, notes, and snippets.

@Sythelux
Created September 12, 2014 15:35
Show Gist options
  • Save Sythelux/9d486d3208bee203fc55 to your computer and use it in GitHub Desktop.
Save Sythelux/9d486d3208bee203fc55 to your computer and use it in GitHub Desktop.
A Class that can resize an Array (see oldArray in Code) to larger Arrays
public static void main(String[] args) {
int[] oldArray = new int[]{5, 6,
8, 4};
int[] newArray = new int[(int) Math.pow(2, 16)];
int halfNewLength = newArray.length / 2;
int rootNewLength = (int) Math.sqrt(newArray.length);
int rootHalfNewLength = rootNewLength / 2;
System.out.printf("%1$2d %2$2d %3$2d %4$2d\n",newArray.length,halfNewLength,rootNewLength,rootHalfNewLength);
for (int i = 0; i < halfNewLength; i+=rootNewLength) {
for (int j = 0; j < halfNewLength; j++) {
if (j<rootHalfNewLength) {
newArray[j+i] = oldArray[0];
newArray[j+i + halfNewLength] = oldArray[2];
newArray[j+i+rootHalfNewLength] = oldArray[1];
newArray[j+i + halfNewLength +rootHalfNewLength] = oldArray[3];
}
}
}
StringBuilder sb = new StringBuilder("{\n");
for (int i = 0; i < newArray.length; i++) {
sb.append(newArray[i]).append(",");
if ((i + 1) % rootNewLength == 0) {
sb.append("\n");
}
}
sb.append("}");
System.out.println(sb.toString());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment