scala 字符串数组

Scala array is a collection of elements of the same data type. The data type can be string also, this is a string array.

Scala数组是相同数据类型的元素的集合。 数据类型也可以是字符串,这是一个字符串数组。

    array = {"Hello!", "IncludeHelp", "Scala"}

在Scala中创建字符串数组 (Creating a string array in Scala)

You can initialize an array using the Array keyword. To know more about creating a Scala array, read: Scala Arrays

您可以使用Array关键字初始化数组。 要了解有关创建Scala数组的更多信息,请阅读: Scala Arrays

There are multiple ways to create a string array in Scala.

在Scala中有多种创建字符串数组的方法。

Method 1:

方法1:

If you know all elements of the array in advance, you can initialize the Scala array in the following manner.

如果您事先知道数组的所有元素,则可以按照以下方式初始化Scala数组。

    val arr_name = Array(element1, element2, ...)

Program:

程序:

object myObject
{
def main(args:Array[String])
{
val colors = Array("Red", "Blue", "Black", "Green")
println("This is an array of string :")
for(i <- 0 to colors.length-1){println(colors(i))
}
}
}

Output

输出量

This is an array of string :
Red
Blue
Black
Green

Method 2:

方法2:

If you don't know the elements of the string in advance but know the number of elements present in the array, then we will create an empty array of definite size first then feed elements to it.

如果您不事先知道字符串的元素,但是知道数组中存在的元素数量,那么我们将首先创建一个确定大小的空数组,然后将元素馈入其中。

    val arr_name = new Array[String](number_of_elements)

Program:

程序:

object myObject
{
def main(args:Array[String])
{
val colors = new Array[String](3)
colors(0) = "Red"
colors(1) = "Blue"
colors(2) = "Black"
println("This is an array of string :")
for(i <- 0 to colors.length-1){println("colors("+i+") : "+colors(i))
}
}
}

Output

输出量

This is an array of string :
colors(0) : Red
colors(1) : Blue
colors(2) : Black

在Scala中创建可变字符串数组 (Creating Mutable Strings Array in Scala)

You can create a string array even when you do not know the size and element of the array. For this, we will create a mutable string. This mutable string is created using

即使不知道数组的大小和元素,也可以创建字符串数组。 为此,我们将创建一个可变字符串。 该可变字符串是使用以下命令创建的

ArrayBuffer class whereas others were created using Array class.

ArrayBuffer类,而其他类是使用Array类创建的。

To create a mutable string we need to import mutable.ArrayBuffer in your program using,

要创建可变字符串,我们需要使用以下命令在您的程序中导入mutable.ArrayBuffer :

    import scala.collection.mutable.ArrayBuffer

Program:

程序:

import scala.collection.mutable.ArrayBuffer
object myObject
{
def main(args:Array[String])
{
val colors = new ArrayBuffer[String]()
colors += "Red"
colors += "Blue"
colors += "Black"
colors += "Green"
println("Mutable string array:")
for(i <- 0 to colors.length-1){println("colors("+i+") : " + colors(i))
}
}
}

Output

输出量

Mutable string array:
colors(0) : Red
colors(1) : Blue
colors(2) : Black
colors(3) : Green

翻译自: https://www.includehelp.com/scala/program-to-create-strings-array.aspx

scala 字符串数组

scala 字符串数组_Scala程序创建字符串数组相关推荐

  1. scala 拆分字符串翻转_Scala程序分割字符串

    scala 拆分字符串翻转 A string is a collection that stores multiple characters, it is an immutable sequence ...

  2. java正确定义一维数组_java一维数组定义,创建一维数组详解

    前面给大家介绍了一下什么是java数组,下面要接着给大家介绍的就是java一维数组定的定义以及创建一维数组方面的知识. 数组当中的每个元素都只带有一个下标的时候,这样的数组就是一维数组,一维数组实质上 ...

  3. scala反转字符串_Scala程序反转字符串

    scala反转字符串 反转字符串 (Reversing a string) Logically, reversing is swapping the values from index 1 with ...

  4. Scala中的ArrayBuffer(创建可变数组)

    Scala中的ArrayBuffer (ArrayBuffer In Scala) In Scala, arrays are immutable and contain homogenous elem ...

  5. 创建php数组的方法,创建PHP数组的方法讲解

    在本文将数组的各种创建方式用PHP实例代码的方式分享给大家,感兴趣的朋友可以了解下 看这篇文章之前相信大家都已经看过PHP中文手册关于数组这一节的讲解了,怎么样呢,看懂了多少?至少我第一次阅读文档时是 ...

  6. python定义一个空数组_python如何创建空数组?

    Python创建空数组的三种方式: 1.numpy指定形状为0 实际上,empty生成的数组当然可以为空,只要我们指定了相应的形状.例如,如果我们传入数组的形状参数为(0,3),则可以生成目标空数组: ...

  7. matlab一维数组截取,Matlab创建一维数组的具体操作讲解

    原创Matlab创建一维数组的具体操作讲解 编辑:小葫芦 来源:PC下载网时间:2019-12-25 11:54:41 最近不少网友表示自己刚刚入手Matlab软件,还不会创建一维数组,而下面这篇文章 ...

  8. python定义一个字符串变量_python怎么创建字符串变量

    python怎么创建字符串变量? 字符串是 Python 中最常用的数据类型.我们可以使用引号('或")来创建字符串. 创建字符串很简单,只要为变量分配一个值即可.例如:var1 = 'He ...

  9. scala 字符串函数_Scala中的字符串chomp(或chop)函数

    scala 字符串函数 剁或剁弦 (Chop or Chomp string) It is used to chop off the end of line characters. For this ...

最新文章

  1. IDisposable实现的方法什么时候被执行的
  2. 人生最浪费生命的四件事,2017年别再做了!
  3. Linux 文件查找(find)
  4. 在config.php文件中,写好了config文件怎么在PHP文件里按条件查询
  5. 修改UITextField中的placeholder的字体
  6. 显示当前没有家庭组计算机,已创建家庭组且加入家庭组,但显示“当前没有其他可用的家庭组计算机?”...
  7. 百度AI学习:一、语音识别
  8. oracle回滚断查询,Oracle回滚段使用查询代码详解
  9. js 使用image.height和image.width获取图片宽高值为0,获取失败
  10. 雕刻在LINUX内核中的LINUS故事
  11. 初了解IsPostBack
  12. kubeedge源码分析
  13. 两个for循环写出大小写字母表
  14. c语言高级成分,高级语言的基本成分数据成分,运算成分,控制成分,传输成分,怎么看它们的类型区别的?比如其中对处理对象的类型说明属于高级语...
  15. 《生物化学与分子生物学》----糖酵解,柠檬酸---听课笔记(十五)
  16. 投融资项目入门和总结
  17. 2023年,把“软件评测师”考起来吧~
  18. 直方图和柱状图的区别有哪些
  19. ipad iphone开发_如何在iPhone或iPad上关闭手机数据
  20. python自学-class14(down)-窗口界面的设计基础

热门文章

  1. Redis的启动方式三种
  2. 树莓派4B安装omv6+omvextrasorg
  3. Webpack 大法之 Code Splitting
  4. 整数转罗马数字C++代码实现
  5. 宇视网络视频录像机运动检测告警推送到手机客户端
  6. Docker 安装部署 centOS8.5 详细步骤
  7. ROS1结合自动驾驶数据集Kitti开发教程(四)画出自己车子模型以及照相机视野
  8. htcvive怎么输入_Steam VR(HTCVIVE)按键调用
  9. 网站的内容百度为什么不收录?
  10. 电子表格软件怎么选?