どうして
while(!s1.isEmpty() && !s2.isEmpty())
と
while(!(s1.isEmpty() && s2.isEmpty()))
同じじゃない?
!(x&y) eclipse を実行するとエラーが発生したためです。
http://pastebin.com/8zBK1jQv
エラーメッセージ:
Exception in thread "main" java.lang.NullPointerException
どうして
while(!s1.isEmpty() && !s2.isEmpty())
と
while(!(s1.isEmpty() && s2.isEmpty()))
同じじゃない?
!(x&y) eclipse を実行するとエラーが発生したためです。
http://pastebin.com/8zBK1jQv
エラーメッセージ:
Exception in thread "main" java.lang.NullPointerException
This is one of De Morgan's Law's, fundamental to Boolean algebra:
(NOT A) AND (NOT B) == NOT (A OR B)
and its converse:
(NOT A) OR (NOT B) == NOT (A AND B)
i.e, to fix your second version, you must replace the &&
with ||
.
真理値表をコンパイルして、自分の目で確かめてください。
while(!s1.isEmpty() && !s2.isEmpty()):
false, false -> true
false, true -> false
true, false -> false
true, true -> false
while(!(s1.isEmpty() && s2.isEmpty())):
false, false -> true
false, true -> true
true, false -> true
true, true -> false
上記からわかるように、2つの条件の意味は大きく異なります。
また、&&
短絡しているため、s2.isEmpty()
評価するかどうかが異なります。最初のステートメントはそれをiffと評価しs1.isEmpty() == false
、2番目のステートメントはそれをiffと評価しs1.isEmpty() == true
ます。である場合、これはあなたが得るかどうかを決定する可能性がs2
あります。null
NullPointerException
この表を確認してください (1 = true、0 = false としましょう)
A|B| !A | !B | !A and !B | A and B | not(A and B)
-+-+----+----+-----------+---------+---------------
0|0| 1 | 1 | [1] | 0 | [1]
0|1| 1 | 0 | [0] | 0 | [1] <- (!A and !B) != not(A and B)
1|0| 0 | 1 | [0] | 0 | [1] <- (!A and !B) != not(A and B)
1|1| 0 | 0 | [0] | 1 | [0]
A=1, B=0
またはでわかるように、 とA=0, B=1
では異なる結果に!A and !B
なりnot(A and B)
ます。
今すぐチェックしましょうnot(A or B)
A|B| !A | !B |[!A and !B]| A or B | not(A or B)
-+-+----+----+-----------+---------+---------------
0|0| 1 | 1 | [1] | 0 | [1]
0|1| 1 | 0 | [0] | 1 | [0]
1|0| 0 | 1 | [0] | 1 | [0]
1|1| 0 | 0 | [0] | 1 | [0]
!A **AND** !B
を次のように置き換えることができる理由がわかります。not(A **OR** B)
NOT
1 -> 0
0 -> 1
s1.isEmpty()
とTrue
しましょs2.isEmpty()
うFalse
ケース 1の場合:
while(!s1.isEmpty() && !s2.isEmpty())
.
その後、 のように扱わ(False AND True)
れ、最終的に が返されFalse
ます。なぜなら、 と の操作AND
はです。True
False
True
ケース 2の場合:
while(!(s1.isEmpty() && s2.isEmpty()))
.
次に、 のように扱い(!(True AND False))
、戻り値(!(False))
とNot
操作はFalse
になりますTrue
。
while(!s1.isEmpty() && !s2.isEmpty())
と
while(!(s1.isEmpty() || s2.isEmpty()))
等しいです。
//「||」に注意してください
非常に重要
ですが、モーガンの規則では、これら 2 つのステートメントは論理的に同じであることが示されています。ほとんどの言語は、持っているものだけを評価します。したがって、条件の一部に副作用がある場合、または他の部分が有効な場合にのみアクセスできる場合は、部分の順序を考慮する必要があります。