This question already has an answer here:
这个问题在这里已有答案:
- Slice notation in Scala? 4 answers
Scala中的切片表示法? 4个答案
In Python, you can get a sublist from a certain starting index to the end using x[i:]
, for example if x = [1, 2, 3, 4], x[1:]
would give you [2, 3, 4]
. I was wondering what is the equivalent way to do this in Scala.
在Python中,您可以使用x [i:]从某个起始索引到结尾获取子列表,例如,如果x = [1,2,3,4],x [1:]会给你[2,3] ,4]。我想知道在Scala中执行此操作的等效方法是什么。
2 个解决方案
#1
2
Use drop
for this case:
在这种情况下使用drop:
x.drop(1)
For the particular case of 1, we usually use x.tail
instead.
对于1的特定情况,我们通常使用x.tail代替。
#2
1
It's called drop
and you use it like xs.drop(i)
.
它被称为drop,你可以像xs.drop(i)一样使用它。
#1
2
Use drop
for this case:
在这种情况下使用drop:
x.drop(1)
For the particular case of 1, we usually use x.tail
instead.
对于1的特定情况,我们通常使用x.tail代替。
#2
1
It's called drop
and you use it like xs.drop(i)
.
它被称为drop,你可以像xs.drop(i)一样使用它。