Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private static boolean isEvenArray(Object array, int deep, int[] lengths) {
- if (array == null) {
- throw new IllegalArgumentException();
- } else if (array instanceof boolean[]) {
- if (lengths[deep] == -1) {
- lengths[deep] = ((boolean[]) array).length;
- } else return lengths[deep] == ((boolean[]) array).length;
- } else if (array instanceof char[]) {
- if (lengths[deep] == -1) {
- lengths[deep] = ((char[]) array).length;
- } else return lengths[deep] == ((char[]) array).length;
- } else if (array instanceof short[]) {
- if (lengths[deep] == -1) {
- lengths[deep] = ((short[]) array).length;
- } else return lengths[deep] == ((short[]) array).length;
- } else if (array instanceof int[]) {
- if (lengths[deep] == -1) {
- lengths[deep] = ((int[]) array).length;
- } else return lengths[deep] == ((int[]) array).length;
- } else if (array instanceof long[]) {
- if (lengths[deep] == -1) {
- lengths[deep] = ((long[]) array).length;
- } else return lengths[deep] == ((long[]) array).length;
- } else if (array instanceof float[]) {
- if (lengths[deep] == -1) {
- lengths[deep] = ((float[]) array).length;
- } else return lengths[deep] == ((float[]) array).length;
- } else if (array instanceof double[]) {
- if (lengths[deep] == -1) {
- lengths[deep] = ((double[]) array).length;
- } else return lengths[deep] == ((double[]) array).length;
- } else if (array.getClass().isArray()) {
- if (lengths[deep] == -1) {
- lengths[deep] = ((Object[]) array).length;
- } else if (lengths[deep] != ((Object[]) array).length) {
- return false;
- }
- if (lengths[deep] > 0) {
- if (lengths[deep] == -1) {
- lengths[deep] = ((Object[]) array).length;
- } else if (lengths[deep] != ((Object[]) array).length) {
- return false;
- }
- ++deep;
- if (deep != 1) {
- for (Object array1 : ((Object[]) array)) {
- if (array1.getClass().isArray()) {
- isEvenArray(array1, deep, lengths);
- }
- }
- } else {
- boolean res = true;
- for (Object array1 : ((Object[]) array)) {
- if (!isEvenArray(array1, deep, lengths)) {
- res = false;
- }
- }
- return res;
- }
- }
- }
- return true;
- }
- public static int calculateDeepArray(Object array) {
- Class<?> cl = array.getClass();
- int dimensions = 0;
- if (cl.isArray()) {
- do {
- dimensions++;
- cl = cl.getComponentType();
- } while (cl.isArray());
- }
- return dimensions;
- }
- public static boolean isJaggedArray(Object array) {
- int[] result = new int[calculateDeepArray(array)];
- Arrays.fill(result, -1);
- return isEvenArray(array, 0, result);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement