2 回答

TA貢獻1818條經(jīng)驗 獲得超7個贊
null與空不同。列表的大小為 1,這意味著有 1 個對象(即使它的所有屬性都為 null)。如果要檢查其所有屬性是否為空,則可以獲取每個屬性并檢查它是否為空。
您可以使用其他方法來確定屬性是否為 not 的 null,然后只調(diào)用它。例如:
public class DomainClass1 {
String str1, str2;
public boolean isEmpty() {
if (this.str1 != null && this.str2 != null) {
return false;
} else {
return true;
}
}
}
現(xiàn)在調(diào)用它,如下所示:
for (int i = 0; i < domain.size(); i++) {
if (domain.get(i).isEmpty()) {
// all fields null
} else {
// not all are null
}
}

TA貢獻1864條經(jīng)驗 獲得超6個贊
domain.isEmpty()
它只是驗證列表是空的,它不驗證d的字段是空的,你可以在DomainClass1類中添加一個方法,就像這樣
public class DomainClass1 {
private String test1;
private String test2;
private String test3;
private String test4;
...
public boolean isEmpty() {
if(test1 != null || test2 != null || test3 != null || test4 != null) {
return false;
}
return true;
}
}
那么你可以使用 d.isEmpty() 來替換 domain.isEmpty()
添加回答
舉報