Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/395.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
什么';这个表达式在Java中的含义是什么?_Java - Fatal编程技术网

什么';这个表达式在Java中的含义是什么?

什么';这个表达式在Java中的含义是什么?,java,Java,我正在学习《Java教程》第六版。 我浏览了这个例子: public int indexOf(E e) { for (ListIterator<E> it = listIterator(); it.hasNext(); ) if(e == null ? it.next() == null : e.equals(it.net())) return it.previousIndex(); return -1; } public i

我正在学习《Java教程》第六版。 我浏览了这个例子:

public int indexOf(E e) {
    for (ListIterator<E> it = listIterator(); it.hasNext(); )
        if(e == null ? it.next() == null : e.equals(it.net()))
            return it.previousIndex();
    return -1;
}
public int indexOf(E){
for(ListIterator it=ListIterator();it.hasNext();)
如果(e==null?it.next()==null:e.equals(it.net())
返回它。previousIndex();
返回-1;
}
我的问题是:for循环使用的特定语法的确切含义是什么?那么,在if条件下“?”和“:”

简单来说

minVal = (a < b) ? a : b;
意思是

if e== null, 
   execute it.next() ==  null // compares and return true/false
else 
   execute e.equals(it.net()) // compares and return true/false
for(初始化;条件;增量){…}
是正常的for循环语法。如果不需要,可以将递增部分留空。但要小心,因为这可能会导致无休止的循环。这就是你的循环的样子——没有递增部分

e==null?it.next()==null:e.equals(it.net())
是一个简单的三元运算符:
IF条件?然后:ELSE

重写它的意思是这样的:

if(e == null) {
    return it.next == null 
}  else {
    return e.equals(it.net())
}

当它返回一个布尔值时,封闭的
if()
语句接受它作为条件。

我投票结束这个问题,因为这里没有什么需要修正的。请阅读关于三元运算符的内容。我想了解一下语法的含义。我不认为这是离题的。首先,你必须学习
for
循环及其工作原理,然后学习
if
条件和
三元运算符。谢谢,我读了三元运算符的含义。现在的问题是:它是如何组成的?我知道有三个条件的经典for循环
if(e == null) {
    return it.next == null 
}  else {
    return e.equals(it.net())
}