小编Pav*_*nko的帖子

为什么在 Java 中声明一个非空数组的空数组是合法的?

好奇为什么在 Java 中声明一个非空数组的空数组是合法的:

int[][] array = new int[0][1];

System.out.println(array[][0]); //won't compile.
System.out.println(array[0][0]) //triggers an out of bounds exception.
Run Code Online (Sandbox Code Playgroud)

PS 我已经阅读了关于零大小数组的相关问题:为什么 Java 允许大小为 0 的数组?

这是出于同样的原因吗?

java arrays multidimensional-array

6
推荐指数
1
解决办法
231
查看次数

在 JUnit 5 参数化测试中。CSV 输入。有没有办法传递 Double.NaN、Double.POSITIVE_INFINITY?

我正在尝试将Double.NEGATIVE_INFINITY,Double.POSITIVE_INFINITYDouble.NaN作为 JUnit5 中的 CSV 参数传递:

@DisplayName("Testing ActivationFunction.heaviside")
@ParameterizedTest(name = "Testing ActivationFunction.heaviside ({0},{1})")
@CsvSource({
    "Double.NEGATIVE_INFINITY, 0",
    "-100, 0",
    "-1, 0",
    "0, 0.5",
    "1, 1",
    "100, 1",
    "Double.POSITIVE_INFINITY, 0",
    "Double.NaN, Double.NaN"
})
void testActivationFunction_heaviside(double input, double output) {

    assertEquals(output, ActivationFunction.heaviside(input));

}
Run Code Online (Sandbox Code Playgroud)

不幸的是,它会Error converting parameter at index 0: Failed to convert String "Double.POSITIVE_INFINITY" to type java.lang.Double在 JUnit5 测试运行程序中触发类似“”的错误。有没有一种好方法可以自动传递这些值进行测试,或者我只需编写一个单独的测试方法,如下所示:

assertEquals(0, Double.NEGATIVE_INFINITY);
assertEquals(1, Double.POSITIVE_INFINITY);
assertEquals(Double.NaN, Double.NaN);
Run Code Online (Sandbox Code Playgroud)

java unit-testing junit5

5
推荐指数
1
解决办法
1935
查看次数

在Scheme中的相互递归中获得尾调用优化

在为MIT/GNU Scheme (rel 9.2 ) 中的oddeven函数开发经典练习代码时,我遇到了一个问题,即我的代码不会因大整数值而终止。首先,我测试了以下代码,它处理正值和负值:

(define error-message-number "Error. x must be a number")

(define odd?
  (lambda (x)
    (cond 
      ((not (integer? x)) error-message-number)
      ((= x 0) #f)
      ((< x 0) (even? (+ x 1))) ;for negatives
      (else (even? (- x 1)))))) ;if number n is odd then n - 1 is even 

(define even?
  (lambda (x)
    (cond 
      ((not (integer? x)) error-message-number)
      ((= x 0) #t)
      ((< x 0) (odd? (+ x 1))) ;for negatives
      (else …
Run Code Online (Sandbox Code Playgroud)

recursion scheme tail-recursion mutual-recursion mit-scheme

0
推荐指数
1
解决办法
141
查看次数