scala 字符串数组_如何创建一系列字符作为Scala数组?

scala 字符串数组

For storing a sequence of characters in an array, there is a quick trick that can be used in Scala programming. In this tutorial, we will show you a working example of how to initialize a character array with a range of characters?

为了将字符序列存储在数组中 ,有一个快速技巧可以在Scala编程中使用。 在本教程中,我们将向您展示一个有效的示例,该示例如何初始化带有一系列字符的字符数组?

An array is a sequence of data elements of the same data type. We can also make a char array, and in this tutorial, we will create a character array.

数组是具有相同数据类型的数据元素序列。 我们还可以创建一个char数组,在本教程中,我们将创建一个字符数组。

The range is a way to create a sequence of elements from a starting element to an ending element. this can be used to create a sequence of numbers as well as characters.

范围是一种创建从开始元素到结束元素的元素序列的方法。 可以用来创建数字和字符的序列。

创建一个字符序列作为Scala数组 (Creating a sequence of characters as a Scala array)

We can create a sequence of characters as a Scala array using Scala method along with their range keyword. To the range, we will pass the starting and ending characters of the sequence. This will give out a sequence with elements from starting to ending characters.

我们可以使用Scala方法及其range关键字将字符序列创建为Scala数组。 对于该范围,我们将传递序列的开始和结束字符。 这将给出从开始到结束字符的元素顺序。

For example, if we pass 's' and 'x' to the range it will return 's, t, u, v, w, x'. And we will store this to an array of characters.

例如,如果我们将“ s”和“ x”传递到范围,它将返回“ s,t,u,v,w,x”。 然后将其存储到字符数组中。

There are two ways to assign a range to an array:

有两种方法可以将范围分配给数组:

  1. Using ().toArray

    使用().toArray

  2. Using array.range()

    使用array.range()

1) Creating a sequence of characters as a Scala array using ().toArray

1)使用().toArray将字符序列创建为Scala数组

The toArray() method is used to convert the given object to the array. This will create an array using the data type of the first occurrence datatype.

toArray()方法用于将给定的对象转换为数组。 这将使用第一个出现的数据类型的数据类型创建一个数组。

Program:

程序:

object MyClass {
    def main(args: Array[String]) {
        var arr = ('s' to 'x').toArray
        arr.foreach { println }
    }
}

Output

输出量

s
t
u
v
w
x

2) Creating a sequence of characters as a Scala array using array.range()

2)使用array.range()创建一个字符序列作为Scala数组

The output of array.range() is also the same as the toarray(), but is a different method to do the work.

array.range的输出()也是一样的指定者(),但是不同的方法来完成工作。

Program:

程序:

object MyClass {
    def main(args: Array[String]) {
        val arr = Array.range('a', 'e')  
        arr.foreach { println }
    }
}

Output

输出量

97
98
99
100

It will give out the ASCII value of the characters. Now, you can use can them to characters explicitly to given char output.

它将给出字符的ASCII值。 现在,您可以使用它们可以将字符显式地赋予给定的char输出。

翻译自: https://www.includehelp.com/scala/how-to-create-a-range-of-characters-as-a-scala-array.aspx

scala 字符串数组

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值