检查数组是否参差不齐 [英] Checking to see if an array is jagged

查看:89
本文介绍了检查数组是否参差不齐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在准备一个程序,但是最后一个方面有点麻烦.

I am finishing up a program but having a bit of trouble with one last aspect.

在程序的这一部分中,我正在测试以查看数组是否呈锯齿状(相同的行数和列数).我想使用嵌套的 for 循环来执行此操作,但是在逻辑和结构上遇到了麻烦.

In this part of the program, I am testing to see if the array is jagged (same number of rows and columns). I want to use a nested for loop to do this but am having trouble with the logic and structure.

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

For example, the following array is jagged:

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

以下数组不是:

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

任何人都可以提供有关此操作的指导吗?

Can anyone offer guidance on how to do this?

推荐答案

首先要弄清楚锯齿状的数组是什么(通常情况下,坚持使用2D数组):

Start with being clear about what a jagged array is (sticking to 2D arrays as the typical case):

  1. 从技术上讲 锯齿状的数组是一个(1D)数组的数组,每个数组可以具有不同的长度.
  2. 人们通常所说的锯齿状数组"(包括我认为的意思)是一个数组,其中包含(1D)个数组元素,它们的长度会有所不同,即 em>有效地呈锯齿状".
  3. 最后,技术上参差不齐但具有相同行数和列数"的数组(有效地)是
  1. Technically a jagged array is an array of (1D) arrays, each of which can have a different length.
  2. Often what people mean by "jagged array" (including you I think) is an array with (1D) array elements that do vary in length - i.e. "effectively jagged".
  3. Finally, an array that is technically jagged but has the "same number of rows and columns" is (effectively) a square array.

(请注意,有效的锯齿形和有效的方阵是互斥的.)

(Notice that effectively jagged and effectively square arrays are mutually exclusive.)

您不需要嵌套的 for 循环来检查以下三个条件中的任何一个:

You do not need nested for loops to check for any of these three conditions:

  • 条件1通过 int [] [] 声明是不言而喻的.
  • 条件2和条件3要求一个 for 循环-因为您不需要遍历包含可能长度不同的数组可能具有不同长度的数组,只需遍历前者并检查后者的长度即可.
  • Condition 1 is self-evident by virtue of a int[][] declaration.
  • Conditions 2 and 3 necessitate one for loop - because you do not need to iterate through the array that contains the potentially different-length arrays and the potentially different-length arrays, just iterate through the former and check the lengths of the latter.

话虽如此,请考虑以下关于条件2和3的 IsJagged IsSquare 实现和演示:

Having said this, consider the following IsJagged and IsSquare implementations and demo with respect to conditions 2 and 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 ");
    }
}

如果运行演示,应该看到以下输出:

If you run the demo, you should see the following output:

a is not jagged and is not square.
b is not jagged and is square.
c is not jagged and is square.
d is jagged and is not square.
e is not jagged and is not square.
f is not jagged and is not square.

这篇关于检查数组是否参差不齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆