Skip to content

Instantly share code, notes, and snippets.

@freewind
Created November 25, 2011 13:53
Show Gist options
  • Save freewind/1393582 to your computer and use it in GitHub Desktop.
Save freewind/1393582 to your computer and use it in GitHub Desktop.
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Date;
public class TestArr {
public static <T> T[][] createArray(int rows, int cols, T init) {
T[] row = (T[]) Array.newInstance(init.getClass(), cols);
Arrays.fill(row, init);
T[][] result = (T[][]) Array.newInstance(row.getClass(), rows);
Arrays.fill(result, row);
return result;
}
public static void main(String[] args) {
int rows = 2;
int cols = 8;
Date[][] arr = createArray(rows, cols, new Date());
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
Date item = arr[i][j];
System.out.println(item);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment