Java Array Length Get lengthArray(Object[][] arr)

Here you can find the source of lengthArray(Object[][] arr)

Description

Returns an array of the lengths of the sub-arrays of a given array.

License

Open Source License

Parameter

Parameter Description
arr a two-or-more-dimensional array

Return

a length array as described above, or null if arr is null

Declaration

public static int[] lengthArray(Object[][] arr) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**/*from   ww w.  jav a  2  s  . c  om*/
     * Returns an array of the lengths of the sub-arrays of a given array.
     * 
     * <p>
     * That is, returns an array <code>ret</code> such that:
     * </p>
     * 
     * <ul>
     * <li><code>ret.length == arr.length</code>, and</li>
     * <li>for all integers <code>i</code> where <code>0 &lt; i &lt; arr.length</code>, <code>ret[i] = arr[i].length</code>. The length of a null array is defined to be 0.</li>
     * </ul>
     * 
     * @param arr a two-or-more-dimensional array
     * @return a length array as described above, or null if <code>arr</code> is null
     */
    public static int[] lengthArray(Object[][] arr) {
        if (arr == null) {
            return null;
        }
        int[] ret = new int[arr.length];
        for (int i = 0; i < arr.length; i++) {
            ret[i] = (arr[i] != null) ? arr[i].length : 0; // null array has 0 length
        }
        return ret;
    }
}

Related

  1. length(Object[] array)
  2. length(Object[] buf)
  3. length(String[] arr)
  4. length(T[] a)
  5. length3(float[] a)
  6. lengthCheck(double[] src, double[] dest)
  7. lengthCheck(final byte[] buffer, final byte length)
  8. lengthNeeded(byte[] b)
  9. lengthOf(T[] array)