Groovy数组。 [[],[]]。toArray()和[[],[]] *。toArray()之间的区别

时间:2017-08-16 11:58:05

标签: arrays groovy

我已在@Parameterized.Parameters static List data() { [ ['20101113', 'DATE', '20101114', 'DATE', 'some statement', '20101101T000000/20110101T000000', null, ['some', 'expected', 'data', 'results']] // ...and other ]*.toArray() } 中为参数化单元测试准备了数据:

]*.toArray()

它正常工作。我检查了此代码,并尝试使用].toArray()简化替换java.lang.Exception: FirstParametrizedTest.data() must return a Collection of arrays. at org.junit.runners.Parameterized$TestClassRunnerForParameters.computeParams(Parameterized.java:94) at org.junit.runners.Parameterized$TestClassRunnerForParameters.createTest(Parameterized.java:86) at org.junit.runners.BlockJUnit4ClassRunner$1.runReflectiveCall(BlockJUnit4ClassRunner.java:236) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:233) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222) at org.junit.runners.ParentRunner.run(ParentRunner.java:300) at org.junit.runners.Suite.runChild(Suite.java:128) at org.junit.runners.Suite.runChild(Suite.java:24) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222) at org.junit.runners.ParentRunner.run(ParentRunner.java:300) at org.junit.runner.JUnitCore.run(JUnitCore.java:157) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68) at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51) at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:237) at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

].toArray()

我恢复了我的更改并且它有效,但我无法理解为什么指令].toArray()出乎意料? ].toArray()如何运作以及]*.toArray()和{{1}}之间的区别?

1 个答案:

答案 0 :(得分:2)

doc开始,*.被称为扩展运算符并返回一个列表。

  

Spread Operator(*.)用于调用聚合对象的所有项目上的操作。它相当于对每个项目调用操作并将结果收集到列表中:

如果没有*toArray只返回一个对象数组,当然数组不能在运行时强制转换为List。

使用*,您当前的代码会执行以下操作:

  static List data() {
        def result = []
        List input = [
                ['20101113', 'DATE',
                 '20101114', 'DATE',
                 'some statement',
                 '20101101T000000/20110101T000000',
                 null,
                 ['some', 'expected', 'data', 'results']]
                // ...and other
        ]

        for (def item : input) {
            result.add(item != null ? ((List) item).toArray() : null)
        }

        result
    }