Advertisement
Guest User

Untitled

a guest
Sep 21st, 2023
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.37 KB | None | 0 0
  1.   private static boolean isEvenArray(Object array, int deep, int[] lengths) {
  2.         if (array == null) {
  3.             throw new IllegalArgumentException();
  4.         } else if (array instanceof boolean[]) {
  5.             if (lengths[deep] == -1) {
  6.                 lengths[deep] = ((boolean[]) array).length;
  7.             } else return lengths[deep] == ((boolean[]) array).length;
  8.         } else if (array instanceof char[]) {
  9.             if (lengths[deep] == -1) {
  10.                 lengths[deep] = ((char[]) array).length;
  11.             } else return lengths[deep] == ((char[]) array).length;
  12.         } else if (array instanceof short[]) {
  13.             if (lengths[deep] == -1) {
  14.                 lengths[deep] = ((short[]) array).length;
  15.             } else return lengths[deep] == ((short[]) array).length;
  16.         } else if (array instanceof int[]) {
  17.             if (lengths[deep] == -1) {
  18.                 lengths[deep] = ((int[]) array).length;
  19.             } else return lengths[deep] == ((int[]) array).length;
  20.         } else if (array instanceof long[]) {
  21.             if (lengths[deep] == -1) {
  22.                 lengths[deep] = ((long[]) array).length;
  23.             } else return lengths[deep] == ((long[]) array).length;
  24.         } else if (array instanceof float[]) {
  25.             if (lengths[deep] == -1) {
  26.                 lengths[deep] = ((float[]) array).length;
  27.             } else return lengths[deep] == ((float[]) array).length;
  28.         } else if (array instanceof double[]) {
  29.             if (lengths[deep] == -1) {
  30.                 lengths[deep] = ((double[]) array).length;
  31.             } else return lengths[deep] == ((double[]) array).length;
  32.         } else if (array.getClass().isArray()) {
  33.             if (lengths[deep] == -1) {
  34.                 lengths[deep] = ((Object[]) array).length;
  35.             } else if (lengths[deep] != ((Object[]) array).length) {
  36.                 return false;
  37.             }
  38.             if (lengths[deep] > 0) {
  39.                 if (lengths[deep] == -1) {
  40.                     lengths[deep] = ((Object[]) array).length;
  41.                 } else if (lengths[deep] != ((Object[]) array).length) {
  42.                     return false;
  43.                 }
  44.                 ++deep;
  45.                 if (deep != 1) {
  46.                     for (Object array1 : ((Object[]) array)) {
  47.                         if (array1.getClass().isArray()) {
  48.                             isEvenArray(array1, deep, lengths);
  49.                         }
  50.                     }
  51.                 } else {
  52.                     boolean res = true;
  53.                     for (Object array1 : ((Object[]) array)) {
  54.                         if (!isEvenArray(array1, deep, lengths)) {
  55.                             res = false;
  56.                         }
  57.                     }
  58.                     return res;
  59.                 }
  60.             }
  61.         }
  62.         return true;
  63.     }
  64.  
  65.     public static int calculateDeepArray(Object array) {
  66.         Class<?> cl = array.getClass();
  67.         int dimensions = 0;
  68.         if (cl.isArray()) {
  69.             do {
  70.                 dimensions++;
  71.                 cl = cl.getComponentType();
  72.             } while (cl.isArray());
  73.         }
  74.         return dimensions;
  75.     }
  76.  
  77.     public static boolean isJaggedArray(Object array) {
  78.         int[] result = new int[calculateDeepArray(array)];
  79.         Arrays.fill(result, -1);
  80.         return isEvenArray(array, 0, result);
  81.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement