Reputation: 257
If I have a list of ten elements in Scala how can I create a new list that consists of only the elements of the previous list from a range of two indexes. So if the original list was ten items long the new one could be like:
val N=Oldlist(0) to Oldlist(10)
Please do not use the split at method thats not what I'm trying to do.
Upvotes: 1
Views: 3929
Reputation: 1849
List
has a slice(from, to)
method. You should probably use that. I thought it used structural sharing, but it doesn't (as discussed in the comments).
Upvotes: 3
Reputation: 53869
If I understand correctly your question, you can do:
val list = (oldlist(0) to oldList(10)).toList
oldlist(0) to oldList(10)
creates a new Range
that is then converted to a List
.
Upvotes: 0