Java 如果给定索引处的子列表不为null,则返回true,否则返回false
我的锻炼出了问题Java 如果给定索引处的子列表不为null,则返回true,否则返回false,java,arraylist,junit,Java,Arraylist,Junit,我的锻炼出了问题 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 t
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;
}
公共布尔子列表NullCheck(ArrayList列表,int j){
if(list==null | | list.isEmpty()| | list.get(0.isEmpty()){
返回false;
}如果(列表包含(j)){
返回true;
}
返回false;
}
list.contains(j)
如果list
包含值j
,而不是索引j
处的元素不为空,则返回true
你可以写:
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;
}
公共布尔子列表NullCheck(ArrayList列表,int j){
if(list==null){
返回false;
}如果(j>=0&&list.size>j&&list.get(j)!=null,则为else{
返回true;
}
返回false;
}
或者干脆
public boolean subListNullCheck (ArrayList<ArrayList<Integer>> list, int j) {
return list != null && j >= 0 &&& list.size > j && list.get(j) != null;
}
公共布尔子列表NullCheck(ArrayList列表,int j){
返回列表!=null&&j>=0&&list.size>j&&list.get(j)!=null;
}
我仍然得到“java.lang.IndexOutOfBoundsException:索引-1超出长度1的界限”@NicKOver您是否将负j
传递给该方法?在这种情况下,添加一个检查,确保j>=0。