java - 像 array.length 这样不包含 NULL 元素的函数?

标签 java arrays null

TreeNode[] children = grid[row][col].getChildren();

我想要一个简单的函数来告诉我这个数组中有多少个对象? getChildren() 将返回一个大小不大于4的对象,例如:

children[0] = null;
children[1] = TreeNode Object
children[2] = null;
children[3] = null;

最佳答案

你为什么不自己写:

public static <T> int getLength(T[] arr){
    int count = 0;
    for(T el : arr)
        if (el != null)
            ++count;
    return count;
}

关于java - 像 array.length 这样不包含 NULL 元素的函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7466102/

相关文章:

java - 比较 getString() 返回值时的意外结果

ios - 使用 Parse.com 检索新数组 iOS

C# 对象引用未设置为对象的实例

Java:在工作目录之外扩展类文件

java - 无法打开Eclipse "No Java virtual machine was found"

javascript - 如何按对象两个值对对象数组进行排序?

ios - 包含可选的过滤器数组 - SearchBar

sql - 具有空值的外部连接表的累积和

objective-c - iOS:NSString变量返回null

java - 在 linux 中从 c 程序调用 java 程序