7

How can I print the first element in list using Scala?

For example in Python I can just write:

>>>l = [1,2,3,4]
>>>one = l[0]
>>>print one

How can I do that on Scala

Thanks.

4 Answers 4

12

The element can be retrieved using index-based access like this:

object ListDemo extends App {
    val lst = List(1, 2, 3)
    println(lst(0)) // Prints specific value. In this case 1.
                    // Number at 0 position.
    println(lst(1)) // Prints 2.
    println(lst(2)) // Prints 3.
}
Sign up to request clarification or add additional context in comments.

Comments

9

Basically, your python code is equivalent of:

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

scala> val one = l.head
one: Int = 1

scala> println(one)
1

(Run in the scala interpreter.)

Here is the documentation about Scala's List.


It was asked as a subsidiary question «how do I display each element?».

Here is a recursive implementation using pattern matching:

scala> def recPrint(xs: List[Int]) {
     | xs match {
     |     case Nil => // nothing else to do
     |     case head :: tail =>
     |         println(head)
     |         recPrint(tail)
     | }}
recPrint: (xs: List[Int])Unit

scala> recPrint(l)
1
2
3
4

As David Weber pointed out in the comments, if you cannot use a recursive algorithm to visit your list's elements then you should consider using another container, because accessing the i-th element of a List takes O(N).

4 Comments

yeah but how can i print second or third element in list?
Well, that was another question ;-) You can either use the apply method (like in Brano88's answer), or recurse on the tail of the list.
If you can traverse the list from head to tail, then recurse away. If not, you are using the wrong data structure as apply is O(N) for lists. Use Vector or Array instead.
@DavidWeber that's a very good point. I've included it in my answer. ;-)
2

Answer can easily be found in scaladoc for list

def head: A
Selects the first element of this list.

Comments

1

Here we have created an object (Object name: TestList) which containing variable myList.

scala> object TestList{
     |       def main(args: (Array[String])){
     |       var myList = List(1,2,3,4,5)
     |       println("Complete List is : "+myList)
     |       println("Reverse List : "+myList.reverse)
     |       println("Print Send Element from list : " + myList(1))
     |       println("Print First three element : "+ myList.take(3))
     |       println("Remove First two element : "+myList.drop(2))
     |       }
     |       }

defined object TestList

Calling object with main Method

scala> TestList.main(Array(""))

OUTPUT:

    Complete List is : List(1, 2, 3, 4, 5)
    Reverse List : List(5, 4, 3, 2, 1)
    Print Send Element from list : 2
    Print First three element : List(1, 2, 3)
    Remove First two element : List(3, 4, 5)

1 Comment

Welcome to Stack Overflow! Please don't answer just with source code. Try to provide a nice description about how your solution works. See: How do I write a good answer?. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.