Scala中的元组

Scala元组 (Scala Tuples)

A tuple is a data structure that has multiple elements. The elements of Scala tuples may or may not be the same data type.

元组是具有多个元素的数据结构。 Scala元组的元素可以或可以不是相同的数据类型。

The Scala tuples are immutable, i.e the objects of different type can be stored in the tuple but the value of these objects cannot be changed. The maximum number of miscellaneous elements that a tuple can have is twenty-two.

Scala元组是不可变的,即不同类型的对象可以存储在元组中,但是这些对象的值不能更改。 元组可以具有的杂项元素的最大数量为22。

You can easily use tuples in your scala program for usage. The syntax for defining a tuple:

您可以在scala程序中轻松使用元组进行使用。 定义元组的语法:

    val tuple_name = (data1, data2, data3, ...);

This is a shortcut way of defining a tuple. You can define a tuple by mentioning its tuple class, the class is simply tuples, where n is the number of elements the tuple has. For example,

这是定义元组的捷径。 您可以通过提及其tuple类来定义一个 ,该类就是tuples ,其中n是该元组具有的元素数。 例如,

    val tuple_name = new tuple2(data1, data2);

There is one more method to define a tuple by using the -> operator:

还有一种使用->运算符定义元组的方法:

    val tuple_name = 'data1' -> 'data2';

访问元组的元素 (Accessing the elements of a tuple)

The access method for accessing an element of a tuple is quite easy and convenient. To access the element at no. n, you have to type in tuple._n, where the tuple is the name of a tuple and nth element is fetched.

用于访问元组元素的访问方法非常简便。 要不访问元素。 n ,您必须输入tuple._n ,其中tuple是元组的名称,并提取 n 元素。

For example, if we need fetch 6th element of the tuple t then we will type t._6 to access it.

例如,如果我们需要获取元组t的 6 元素,则将键入t._6进行访问。

解释元组的初始化和访问的代码 (Code to explain the initialization and accessing of tuples)

object MyClass {
    def main(args: Array[String]) {
        val student = ("Ram", 10 ,92.4)
        println("Student name : "+student._1+" of the Standard "+student._2+" has scored "+student._3+"%")
    }
}

Output

输出量

Student name : Ram of the Standard 10 has scored 92.4%

一些有用的元组方法及其语法 (Some helpful tuple methods and their syntax)

1) tuple.productIterator()

1)tuple.productIterator()

It is used to iterate over the elements of a tuple.

它用于遍历元组的元素。

Syntax:

句法:

    tuple.productIterator();

2) tuple.toString()

2)tuple.toString()

It is used to concatenate all the elements of the tuple into a string.

它用于将元组的所有元素连接成一个字符串。

Syntax:

句法:

    tuple.toString();

3) tuple.swap()

3)tuple.swap()

It is used to swap two elements of a tuple.

它用于交换元组的两个元素。

Syntax:

句法:

    tuple.swap();

用于说明元组方法的示例代码 (Example code to illustrate methods over a tuple)

object MyClass {
    def main(args: Array[String]) {
        val student = ("Ram", 10);
        student.productIterator.foreach(println);
        var tuplestring = student.toString;
        println("\nThe concatenated string is"+tuplestring);
        val studentswap = student.swap;
        println("The swapped tuple is "+studentswap);
    }
}

Output

输出量

Ram
10

The concatenated string is(Ram,10)
The swapped tuple is (10,Ram)

使用名称访问变量 (Accessing variables using names)

For ease of access, you can optionally assign the elements of a tuple name which can later be used to call them. This method is quite useful when you have a handful of elements in your tuple. Remembering names that are logically matching is easier than remembering the number. For example, a tuple has 5 elements and name is 3 elements. To access name, it is easier to remember the word rather than its sequence.

为了便于访问,您可以选择分配一个元组名称的元素,以供以后调用它们。 当元组中有少量元素时,此方法非常有用。 记住逻辑上匹配的名称比记住数字更容易。 例如,一个元组有5个元素,名称是3个元素。 要访问名称,记住单词比记住单词更容易。

Syntax:

句法:

    def student  = ("Ram", 16, 76.12)
    val(name, age, percent) = getUserInfo;


翻译自: https://www.includehelp.com/scala/tuples-in-scala.aspx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值