在 Scala 中获取子数组的正确方法是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10830944/
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 04:12:08  来源:igfitidea点击:

What is the correct way to get a subarray in Scala?

scalascala-collections

提问by nnythm

I am trying to get a subarray in scala, and I am a little confused on what the proper way of doing it is. What I would like the most would be something like how you can do it in python:

我试图在 Scala 中获得一个子数组,但我对正确的做法有点困惑。我最想要的是你如何在 python 中做到这一点:

x = [3, 2, 1]
x[0:2]

but I am fairly certain you cannot do this.

但我相当肯定你不能这样做。

The most obvious way to do it would be to use the Java Arrays util library.

最明显的方法是使用 Java Arrays util 库。

import java.util.Arrays
val start = Array(1, 2, 3)
Arrays.copyOfRange(start, 0, 2)

But it always makes me feel a little dirty to use Java libraries in Scala. The most "scalaic" way I found to do it would be

但是在 Scala 中使用 Java 库总是让我觉得有点脏。我发现的最“scalaic”的方式是

def main(args: List[String]) {
    val start = Array(1, 2, 3)
    arrayCopy(start, 0, 2)
}
def arrayCopy[A](arr: Array[A], start: Int, end: Int)(implicit manifest: Manifest[A]): Array[A] = {
    val ret = new Array(end - start)
    Array.copy(arr, start, ret, 0, end - start)
    ret
}

but is there a better way?

但有更好的方法吗?

回答by paradigmatic

You can call the slice method:

您可以调用 slice 方法:

scala> Array("foo", "hoo", "goo", "ioo", "joo").slice(1, 4)
res6: Array[java.lang.String] = Array(hoo, goo, ioo)

It works like in python.

它就像在 python 中一样工作。

回答by KeyMaker00

Imagine you have an array with elements from ato f

想象一下,你有一个包含从a到的元素的数组f

scala> val array = ('a' to 'f').toArray // Array('a','b','c','d','e','f')

Then you can extract a sub-array from it in different ways:

然后你可以用不同的方式从中提取一个子数组:

  1. Dropping the first n first elements with drop(n: Int)

    array.drop(2) // Array('c','d','e','f')

  2. Take the first n elements with take(n: Int)

    array.take(4) // Array('a','b','c','d')

  3. Select any interval of elements with slice(from: Int, until: Int). Note that untilis excluded.

    array.slice(2,4) // Array('c','d')

    The slice method is stricly equivalent to:
    array.take(4).drop(2) // Array('c','d')

  4. Exclude the last n elements with dropRight(n: Int):

    array.dropRight(4) // Array('a','b')

  5. Select the last n elements with takeRight(n: Int):

    array.takeRight(4) // Array('c','d','e','f')

  1. 删除前 n 个元素 drop(n: Int)

    array.drop(2) // Array('c','d','e','f')

  2. 取前 n 个元素 take(n: Int)

    array.take(4) // Array('a','b','c','d')

  3. 选择任意区间的元素slice(from: Int, until: Int)。请注意,until排除在外。

    array.slice(2,4) // Array('c','d')

    slice 方法严格等同于:
    array.take(4).drop(2) // Array('c','d')

  4. 用 排除最后 n 个元素dropRight(n: Int)

    array.dropRight(4) // Array('a','b')

  5. 选择最后 n 个元素takeRight(n: Int)

    array.takeRight(4) // Array('c','d','e','f')

Reference: Official documentation

参考官方文档