Return 如果给定索引处的子列表不为空,则为真,否则为假

Return true if sub-list at given index is NOT null, false otherwise

我在锻炼时遇到 AssertionError。

public boolean subListNullCheck (ArrayList<ArrayList<Integer>> list, int j) {
    if(list == null || list.isEmpty() || list.get(0).isEmpty() ) {
        return false;
                
    } if(list.contains(j)) {
        return true;
    }
    return false;
}

list.contains(j) returns true 如果 list 包含值 j,如果索引 j 处的元素不为空则不存在.

你可以写:

public boolean subListNullCheck (ArrayList<ArrayList<Integer>> list, int j) {
    if (list == null) {
        return false;             
    } else if (j >= 0 && list.size > j && list.get(j) != null) {
        return true;
    }
    return false;
}

或者干脆

public boolean subListNullCheck (ArrayList<ArrayList<Integer>> list, int j) {
    return list != null && j >= 0 &&& list.size > j && list.get(j) != null;
}