2015-09-10 68 views
-2
Map<Integer, Integer> map = new HashMap<Integer, Integer>(); 

     for (int i = 0; i < array.length; i++) { 
      int temp = array[i]; 
      Integer count = map.get(temp); 

      map.put(temp, (count == null) ? 1 : count + 1); 
     } 

有人可以帮我低估(count == null) ? 1 : count + 1,以上吗?码。这是什么声明:(count == null)? 1:count + 1

+1

这就是三元运算符。它基本上是说“if count == null,give 1. If if not,count count +1” – Emd4600

+1

'?'被称为[ternary operator](https://en.wikipedia.org/wiki/%3F:#Java )。 – Tunaki

+1

https://docs.oracle.com/javase/tutorial/java/nutsandbolts/op2.html – JonK

回答

0

如果count为null,那么它会把1放在map中,否则如果count不为null,它会将count值加1并放到map中。

4

如果countnull,则放1,否则放1 + 1。 这是一个ternary operator。 在一个较长的方式,你可以写

if (count == null) { 
    map.put(temp, 1); 
} else{ 
    map.put(temp, count + 1); 
} 
3

线

map.put(temp, (count == null) ? 1 : count + 1); 

相当于

if (count == null) { 
    map.put(temp, 1); 
} 
else { 
    map.put(temp, count + 1); 
} 
2

对于关键temp值为null,则我们需要把1,否则算+ 1。

相关问题