这些有什么区别?
为什么用一个而不是另一个?
def variable = 5
if( variable ==~ 6 && variable != 6 ) {
return '==~ and != are not the same.'
} else {
return '==~ and != are the same.'
}
Run Code Online (Sandbox Code Playgroud) 在Groovy中尝试浮点运算.不知道为什么/如何/什么groovy在幕后做这些不同类型的行为?
double point2 = 0.2
double point1 = 0.1
double point3 = 0.3
assert point2 + point1 == point3 // false, as expected
| | | | |
0.2 | 0.1 | 0.3
| false
0.30000000000000004
float point2 = 0.2
float point1 = 0.1
float point3 = 0.3
assert point2 + point1 == point3 // false, as expected
| | | | |
0.2 | 0.1 | 0.3
| false
0.30000000447034836
def point2 = 0.2
def point1 = 0.1
def …Run Code Online (Sandbox Code Playgroud) 我看到这段代码有些令人困惑的行为.
def fileArrayMeta = newFile.readLines()
def file3 = fileArrayMeta.collect{ line -> line.split("\\s+") }
println file3[0]
println file3[0].getClass()
Run Code Online (Sandbox Code Playgroud)
这个操作的预期结果应该是.
=> [, NOTE, *ENTRY, STATEMENT, *.]
=> class java.util.ArrayList
Run Code Online (Sandbox Code Playgroud)
这个操作的实际结果是.
=> [, NOTE, *ENTRY, STATEMENT, *.]
=> class [Ljava.lang.String;
Run Code Online (Sandbox Code Playgroud)
为什么会这样?
println file3[0].removeAll("", null)
Run Code Online (Sandbox Code Playgroud)
线程"main"中的异常groovy.lang.MissingMethodException:没有方法签名:[Ljava.lang.String; .removeAll()适用于参数类型:(java.lang.String,null)values:[,null]
我认为这也应该有效,为什么不呢?
def clean4 = clean3.each{it.collect{ it != "" && it != null }}
println clean4[0]
Run Code Online (Sandbox Code Playgroud)