Java 检查二维阵列是否呈锯齿状

Java 检查二维阵列是否呈锯齿状,java,arrays,multidimensional-array,jagged-arrays,Java,Arrays,Multidimensional Array,Jagged Arrays,我正在完成一个程序,但最后一个方面有点麻烦 在程序的这一部分中,我正在测试数组是否参差不齐(行数和列数相同)。我想使用一个嵌套的for循环来实现这一点,但在逻辑和结构上遇到了问题 例如,以下数组是锯齿状的: 1, 2, 3 4, 5, 6 7, 8, 9, 10 而以下数组不是: 1, 2, 3 4, 5, 6 7, 8, 9 有人能提供如何实现这一点的指导吗?首先要弄清楚锯齿阵列是什么(典型情况下坚持使用2D阵列): 从技术上讲,a是一个(1D)数组的数组,每个数组可以有不同的长度 通常人

我正在完成一个程序,但最后一个方面有点麻烦

在程序的这一部分中,我正在测试数组是否参差不齐(行数和列数相同)。我想使用一个嵌套的
for
循环来实现这一点,但在逻辑和结构上遇到了问题

例如,以下数组是锯齿状的:

1, 2, 3
4, 5, 6
7, 8, 9, 10
而以下数组不是:

1, 2, 3
4, 5, 6
7, 8, 9

有人能提供如何实现这一点的指导吗?

首先要弄清楚锯齿阵列是什么(典型情况下坚持使用2D阵列):

  • 从技术上讲,a是一个(1D)数组的数组,每个数组可以有不同的长度
  • 通常人们所说的“锯齿状数组”(我想包括你在内)是指一个具有(1D)个数组元素的数组,这些元素的长度确实不同,即“实际上是锯齿状的”
  • 最后,一个技术上参差不齐但“行数和列数相同”的数组(实际上)是一个数组
  • (请注意,有效锯齿阵列和有效方形阵列是相互排斥的。)

    对于循环,不需要嵌套的
    ,即可检查以下三种情况:

    • 通过
      int[][]
      声明,条件1是不言而喻的
    • 对于
    循环,条件2和3需要一个
    ——因为您不需要遍历包含可能不同长度数组和可能不同长度数组的数组,只需遍历前者并检查后者的长度即可
    
    

    这样说,考虑下面的<代码> > < <代码> > <代码> IsSquare <代码>关于条件2和3的实现和演示:

    public class IsJaggedDemo {
        private static boolean IsJagged(int[][] array) {
            boolean isJagged = false;
    
            if (array != null) {
                Integer lastLength = null;
                for (int i = 0; i < array.length; i++) {
                    if (lastLength == null) {
                        lastLength = array[i].length;
                    } else if (lastLength.equals(array[i].length)) {
                        continue;
                    } else {
                        isJagged = true;
                        break;
                    }
                }
            }
    
            return isJagged;
        }
    
        private static boolean IsSquare(int[][] array) {
            boolean isSquare = false;
    
            if (array != null) {
                for (int i = 0; i < array.length; i++) {
                    if (array[i].length != array.length) {
                        break;
                    } else if (i != array.length - 1) {
                        continue;
                    } else {
                        isSquare = true;
                    }
                }
            }
    
            return isSquare;
        }
    
        public static void main(String[] args) {
            int[][] a = null;
            int[][] b =
                new int[][] {
                    new int[] { 1 }
                };
            int[][] c =
                new int[][] {
                    new int[] { 1, 2, 3 },
                    new int[] { 4, 5, 6 },
                    new int[] { 7, 8, 9 }
                };
            int[][] d =
                new int[][] {
                    new int[] { 1, 2, 3 },
                    new int[] { 4, 5, 6 },
                    new int[] { 7, 8, 9, 10 }
                };
            int[][] e =
                new int[][] {
                    new int[] { 1, 2, 3 }
                };
            int[][] f =
                new int[][] {
                    new int[] { 1, 2, 3 },
                    new int[] { 4, 5, 6 },
                    new int[] { 7, 8, 9 },
                    new int[] { 9, 8, 7 }
                };
    
            System.out.printf(
                    "a is %1$sjagged and is %2$ssquare.\r\n",
                    IsJagged(a) ? "" : "not ",
                    IsSquare(a) ? "" : "not ");
            System.out.printf(
                    "b is %1$sjagged and is %2$ssquare.\r\n",
                    IsJagged(b) ? "" : "not ",
                    IsSquare(b) ? "" : "not ");
            System.out.printf(
                    "c is %1$sjagged and is %2$ssquare.\r\n",
                    IsJagged(c) ? "" : "not ",
                    IsSquare(c) ? "" : "not ");
            System.out.printf(
                    "d is %1$sjagged and is %2$ssquare.\r\n",
                    IsJagged(d) ? "" : "not ",
                    IsSquare(d) ? "" : "not ");
            System.out.printf(
                    "e is %1$sjagged and is %2$ssquare.\r\n",
                    IsJagged(e) ? "" : "not ",
                    IsSquare(e) ? "" : "not ");
            System.out.printf(
                    "f is %1$sjagged and is %2$ssquare.\r\n",
                    IsJagged(f) ? "" : "not ",
                    IsSquare(f) ? "" : "not ");
        }
    }
    

    如果它不是完全必需的,为什么不使用for-each循环而不是嵌套的for循环呢?几分钟前我遇到了这个问题,我认为前面的答案似乎有点复杂,尽管完全正确

    我知道这是一个有几个月历史的问题,但我刚刚提出了一个解决方案:

    public static boolean isJagged(int[][] arr)
    {
        boolean result = false;
        // for each 1D array in the 2D array arr
        for (int[] a : arr)
        {
            if (a.length != arr[0].length)
                result = true;
        }
    
        return result;
    }
    
    如果结果返回为false,则2D数组不是锯齿状的,否则为true

    测试如下:

    public static void main(String[] args)
    {
        int[][] c =
            new int[][] {
                new int[] { 1, 2, 3 },
                new int[] { 4, 5, 6 },
                new int[] { 7, 8, 9 }
            };
        int[][] d =
            new int[][] {
                new int[] { 1, 2, 3 },
                new int[] { 4, 5, 6 },
                new int[] { 7, 8, 9, 10 }
            };
        if (isJagged(c))
            System.out.println("C is jagged");
        else
            System.out.println("C is not jagged");
        if (isJagged(d))
            System.out.println("D is jagged");
        else
            System.out.println("D is not jagged");
    }
    
    这张照片是:

    C is not jagged
    D is jagged
    

    关键是,我认为在嵌套的for循环上使用for-each循环要简单得多。

    在矩形2d数组中,每行中的元素数是相同的,而在锯齿状2d数组中,则不是。在正方形二维数组中,每行中的元素数等于行数

    publicstaticvoidmain(字符串[]args){
    int[]arr1={{1,2,3},{4,5,6},{7,8,9,10};
    int[]arr2={{1,2,3},{4,5,6},{7,8,9};
    System.out.println(isJagged(arr1));//true
    System.out.println(isJagged(arr2));//false
    System.out.println(isSquare(arr2));//true
    }
    
    静态布尔值是交错的(int[]arr){
    int rowLength=arr[0]。长度;
    对于(int[]行:arr)
    if(row.length!=rowLength)
    返回true;
    返回false;
    }
    
    静态布尔isSquare(int[]arr){
    int-length=arr.length;
    对于(int[]行:arr)
    if(row.length!=长度)
    返回false;
    返回true;
    }
    


    另请参见:

    Duplicate:请在发布前搜索网站!话虽如此,@juan.facorro说得恰到好处。
    C is not jagged
    D is jagged