List and Tuples in Scala

前端 未结 5 1920
温柔的废话
温柔的废话 2021-01-27 08:33

From the book \'Programming in Scala\' by Martin Odersky:

Another useful container object is the tuple. Like lists, tuples are immutable, but unlike lis

5条回答
  •  死守一世寂寞
    2021-01-27 09:11

    A very simple demonstration of what the other answers are telling you.

    val tuplX = (10, "ten")      //tuplX: (Int, String) = (10,ten)
    val listX = List(10, "ten")  //listX: List[Any] = List(10, ten)
    
    tuplX._1 - 6     //res0: Int = 4
    tuplX._2.length  //res1: Int = 3
    
    listX(0) - 6     //Error: value - is not a member of Any
    listX(1).length  //Error: value length is not a member of Any
    

提交回复
热议问题