在 Scala 中组合数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7864704/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 03:36:59  来源:igfitidea点击:

Combining Arrays in Scala

scala

提问by pondermatic

If I have DerivedType1:BaseTypeand DerivedType2:BaseTypeand Array[DerivedType1]and Array[DerivedType2], what's the most succinct way of combining them into Array[BaseType]?

如果我有DerivedType1:BaseTypeandDerivedType2:BaseTypeArray[DerivedType1]and Array[DerivedType2],将它们组合成的最简洁的方法是Array[BaseType]什么?

回答by missingfaktor

Use the ++method on Array.

使用 上的++方法Array

scala> class A; class B extends A; class C extends A
defined class A
defined class B
defined class C

scala> Array(new B, new B) ++ Array(new C, new C)
res33: Array[A] = Array(B@b7501b, B@ec5359, C@1540d0c, C@124a927)