Viet Son
Viet Son

Reputation: 143

Scala How to get sublist by index

I have a List(1 ,2 ,3 ,4 ,5) and trying to get a sublist: List(3, 4) from it by the following way:

List(1 ,2 ,3 ,4 ,5).slice(this.size - 3 , this.size - 1 )

But I got an error

error: value size is not a member of object

How can I use "this" parameter in Scala just like in Java. Are there other ways to achieve the goal. Thank you so much.

Upvotes: 14

Views: 33278

Answers (4)

Marcelo Madrazo
Marcelo Madrazo

Reputation: 1

You have a List(1, 2, 3, 4, 5)

1) Is that a Function or Object or a Class?

2) You want to use return values from List( x , x , 3 , 4 , x )

3) you need to use size outside of the parentheses to return the value from your new List function...

4) create a function to complete this step then return it to a variable...

5) when you fire the function List() use variables with List( A , B , C , D , E )

6) all objects must be completely declared before use. for example, How can you use a basketball without air inside? or How can you use a football without being covered in pigskin? or How can you use a computer without a CPU? all components of objects must be completely declared and working properly in programming!

7) All data, functions, objects inside your List() function must be working properly or ... functioning . . .

8) You said "In general I also want to get some elements in the middle" ..

9) As a veteran programmer... You have to know exactly where the elements are in your List() function

10) Also from the start, before all these functions you wish to "use" you must load the classes, that is filled with all the functions you use, at the head of your program.

11) you cannot use a function the same way you use an object or a class!

12) Lastly, you just want to be creative when the programming language sets the rules or boundaries ...Scala is not Java ...

Does this help? with "error: value size is not a member of object"

Upvotes: -3

Biswanath
Biswanath

Reputation: 9185

If you want last 3 or n elements, you can use takeRight(3) or takeRight(n).

Edit based on the question edit:

If you need to not take first f and last l elements then

List(1,2,3,....n).drop(f).dropRight(l) 

For your case it will be List(1,2,3,4,5).drop(2).dropRight(1)

Upvotes: 5

dcastro
dcastro

Reputation: 68660

You should first declare the list, and then refer to the list using its name list, not this:

val list = List(1 ,2 ,3 ,4 ,5)
list.slice(list.size -3, list.size -1)

If you really want to do this in one line, then use reverse, but it's not very efficient:

List(1 ,2 ,3 ,4 ,5).reverse.slice(1, 3).reverse

By the way, that code wouldn't be valid in Java either. this refers to the enclosing object, not to the list.

Upvotes: 19

anquegi
anquegi

Reputation: 11522

The problem is that the word this is not pointing your List, if you want to take some parameter in the middle of the List use take and takeRigth please read the Scala Docs at the end is a link

val my_lst = List(1 ,2 ,3 ,4 ,5)
my_lst.takeRight(3).take(2)

my_lst: List[Int] = List(1, 2, 3, 4, 5)
res0: List[Int] = List(3, 4)

first takeRight with the index counting on right my_lst.size - thepositions, so you must write thepositions and then how many elements you want in your new subList

Here is a explanations of how work the methods in scala List

from Scala Help Doc

def slice(from: Int, until: Int): List[A]

returns

a list containing the elements greater than or equal to index from extending up to (but not including) index until of this list.

Definition Classes List → LinearSeqOptimized → IterableLike → TraversableLike → GenTraversableLike Example:

// Given a list val letters = List('a','b','c','d','e')

// slice returns all elements beginning at index from and afterwards, // up until index until (excluding index until.) letters.slice(1,3) // Returns List('b','c')

in you case use length or size

scala> val my_lst = List(1 ,2 ,3 ,4 ,5)
my_lst: List[Int] = List(1, 2, 3, 4, 5)

scala> my_lst.slice(my_lst.length-3,my_lst.length)
res0: List[Int] = List(3, 4, 5)

but really scala List class has a lot of functions that can fit you. In your case if you want the last 3 elemments use takeRight

def takeRight(n: Int): List[A]

Selects last n elements.

n

the number of elements to take returns

a list consisting only of the last n elements of this list, or else the whole list, if it has less than n elements. 

Definition Classes List → IterableLike

scala> my_lst.takeRight(3)
res2: List[Int] = List(3, 4, 5)

Take a look at the Scala Docs

Upvotes: 0

Related Questions