在挖掘 ArrayList 代码时,我发现了这个代码片段
if ((lst = list) != null && (a = lst.elementData) != null) { ... }
现在我真的不知道这个表达式叫什么,说的是(lst = list) != null
,以及为什么这样使用。 java中的每个赋值都会返回一个分配的对象还是什么?
最佳答案
您基本上是在 lst 中分配 list 的值,然后进行其他检查。上面代码的基本意思是
lst = list;
if ((lst != null && (a = lst.elementData) != null) { ... }
why it is used like this
可能只是为了节省一行代码。它肯定会稍微降低可读性。
旁注:当您在循环内执行某些操作时,此方法非常有用。
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
String line;
while ((line = br.readLine()) != null) {
// process the line.
}
}
在 while 循环中,line 的值在 while 循环的每次迭代后不断更新。
关于java - Java 中赋值操作返回 Object 或 Null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57236578/