ternary

Java autoboxing and ternary operator madness

筅森魡賤 提交于 2019-11-26 11:21:27
问题 Just spent a frustrating couple of hours debugging this code: LinkedHashMap<String, Integer> rsrqs = new LinkedHashMap<String, Integer>(); Integer boxedPci = 52; Integer boxedRsrq = boxedPci != null ? rsrqs.get(boxedPci.toString()) : -1; The above produces a NullPointerException. The below code doesn\'t: LinkedHashMap<String, Integer> rsrqs = new LinkedHashMap<String, Integer>(); Integer boxedPci = 52; Integer boxedRsrq = boxedPci != null ? rsrqs.get(boxedPci.toString()) : Integer.valueOf(-1)

Python Ternary Operator Without else

雨燕双飞 提交于 2019-11-26 09:28:56
问题 Is it possible to do this on one line in Python? if <condition>: myList.append(\'myString\') I have tried the ternary operator: myList.append(\'myString\' if <condition>) but my IDE (MyEclipse) didn\'t like it, without an else . 回答1: Yes, you can do this: <condition> and myList.append('myString') If <condition> is false, then short-circuiting will kick in and the right-hand side won't be evaluated. If <condition> is true, then the right-hand side will be evaluated and the element will be

Java: Ternary with no return. (For method calling)

一世执手 提交于 2019-11-26 08:42:43
I was wondering if it was possible to do a ternary operation but without returning anything. If it's not possible in Java is it possible in other languages, if so which ones apply? name.isChecked() ? name.setChecked(true):name.setChecked(false); No, you can't. But what's the point of this over an if-else statement? Are you really trying to save 7 characters? if (name.isChecked()) { name.setChecked(true); } else { name.setChecked(false); } or if you prefer bad style: if (name.isChecked()) name.setChecked(true); else name.setChecked(false); Never mind the fact that you can just do (in this case)

Strange Java behaviour. Ternary operator

…衆ロ難τιáo~ 提交于 2019-11-26 07:42:55
问题 Why does this code work? Float testFloat = null; Float f = true ? null : 0f; And why does this throw an exception? Float testFloat = null; Float f = true ? testFloat : 0f; But the strangest thing is that this code also runs successfully without any exceptions: Float testFloat = null; Float f = testFloat; It seems that the ternary operator of Java changes the behaviour. Can anyone explain why this is, please? 回答1: The behaviour is specified in JLS - Conditional Operator: If one of the second

Java Ternary without Assignment

倖福魔咒の 提交于 2019-11-26 04:53:47
问题 Is there a way to do a java ternary operation without doing an assignment or way to fake the assingment? I like how succinct ternary code looks when doing a bunch of if/then/elses. I\'m hoping to be able to call one of two void functions based on a boolean algebra statement. Something like: (bool1 && bool2) ? voidFunc1() : voidFunc2(); My functions are of return type void , so if there is a way to fake this in an assignment to make it work, then I\"m okay with that... I would like to see how

?: ternary conditional operator behaviour when leaving one expression empty

旧城冷巷雨未停 提交于 2019-11-26 02:58:19
问题 I was writing a console application that would try to \"guess\" a number by trial and error, it worked fine and all but it left me wondering about a certain part that I wrote absentmindedly, The code is: #include <stdio.h> #include <stdlib.h> int main() { int x,i,a,cc; for(;;){ scanf(\"%d\",&x); a=50; i=100/a; for(cc=0;;cc++) { if(x<a) { printf(\"%d was too big\\n\",a); a=a-((100/(i<<=1))?:1); } else if (x>a) { printf(\"%d was too small\\n\",a); a=a+((100/(i<<=1))?:1); } else { printf(\"%d

Ternary Operators Java

匆匆过客 提交于 2019-11-26 02:02:57
问题 Is there a way to implement this in a ternary operation. I\'m very new to that ternary stuff, maybe you could guide me. if(selection.toLowerCase().equals(\"produkt\")) cmdCse.setVisible(true); else cmdCse.setVisible(false); This one doesn\'t seem to work. selection.toLowerCase().equals(\"produkt\")?cmdCse.setVisible(true):cmdCse.setVisible(false); 回答1: In this case, you don't even need a ternary operator: cmdCse.setVisible(selection.toLowerCase().equals("produkt")); Or, cleaner: cmdCse

Java: Ternary with no return. (For method calling)

守給你的承諾、 提交于 2019-11-26 01:08:32
问题 I was wondering if it was possible to do a ternary operation but without returning anything. If it\'s not possible in Java is it possible in other languages, if so which ones apply? name.isChecked() ? name.setChecked(true):name.setChecked(false); 回答1: No, you can't. But what's the point of this over an if-else statement? Are you really trying to save 7 characters? if (name.isChecked()) { name.setChecked(true); } else { name.setChecked(false); } or if you prefer bad style: if (name.isChecked()