scala中打印数组和集合

Scala中的数组 (Array in Scala)

In Scala, Array is a data structure that is a collection of elements of same data type.

在Scala中,数组是一种数据结构,是相同数据类型的元素的集合。

Creating an array:

创建一个数组:

The Array keyword is used to create an array in Scala. There are multiple syntaxes to create an array. They are,

Array关键字用于在Scala中创建数组。 创建数组有多种语法。 他们是,

    var array_name : Array[data_type] = new Array[data_type(size)
var array_name: Array[data_tpye] = new Array(size)
var array_name = new Array[data_type](size)
var array_name = Array(element1, elmenet2, element3, ...)

All the four declarations are valid but the last two are commonly used.

这四个声明都是有效的,但最后两个是常用的。

Accessing an element from the array:

从数组访问元素:

To access the element of the array, its index is used.

要访问数组的元素,请使用其索引。

Syntax:

句法:

    array_name[index]

打印阵列 (Print Array)

There are two methods to print all the elements of the array (print array):

有两种方法可以打印数组的所有元素(打印数组):

  1. Using iteration

    使用迭代

  2. Using string conversion

    使用字符串转换

1)使用迭代打印数组元素 (1) Printing elements of array using iteration)

We can print the elements of the array by integrating over all the elements of the array and then printing each element using their index.

我们可以通过对数组的所有元素进行积分,然后使用它们的索引来打印每个元素,来打印数组的元素。

Syntax:

句法:

    for(i
Program to print array using iteration

Output:

Elements of Array are :
97 90 89 75 45 78 99
Explanation:
In the above code, we have created an array of integer values and then use the for loop to iterate over the elements of the array using the print statement.

2) Printing elements of the array using string conversion method

We can print array using the string conversion method, i.e. converting the array to a string using mkString() method.
Syntax:
Program to print array using string conversion method
object MyClass {def main(args: Array[String]) {var score = Array("C", "C++", "Java", "Python", "Scala")
var string = score.mkString(" , ")
println("Elements of Array are :\n" + string)
}
}
Output:
Explanation:
In the above code, we have declared an array of strings. We have used the mkString() method to convert the array of string to string and then print it using the print statement.

TOP Interview Coding Problems/Challenges

  • Run-length encoding (find/print frequency of letters in a string)

  • Sort an array of 0's, 1's and 2's in linear time complexity

  • Checking Anagrams (check whether two string is anagrams or not)

  • Relative sorting algorithm

  • Finding subarray with given sum

  • Find the level in a binary tree with given sum K

  • Check whether a Binary Tree is BST (Binary Search Tree) or not

  • 1[0]1 Pattern Count

  • Capitalize first and last letter of each word in a line

  • Print vertical sum of a binary tree

  • Print Boundary Sum of a Binary Tree

  • Reverse a single linked list

  • Greedy Strategy to solve major algorithm problems

  • Job sequencing problem

  • Root to leaf Path Sum

  • Exit Point in a Matrix

  • Find length of loop in a linked list

  • Toppers of Class

  • Print All Nodes that don't have Sibling

  • Transform to Sum Tree

  • Shortest Source to Destination Path



Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.


Please enable JavaScript to view the comments powered by Disqus.

    for(i
 Program to print array using iteration 

 Output: 

Elements of Array are :
97 90 89 75 45 78 99
 Explanation:
In the above code, we have created an array of integer values and then use the for loop to iterate over the elements of the array using the print statement.

2) Printing elements of the array using string conversion method

We can print array using the string conversion method, i.e. converting the array to a string using mkString() method .
 Syntax:
 Program to print array using string conversion method
 object MyClass {
def main ( args : Array [ String ]) {
var score = Array ( "C" , "C++" , "Java" , "Python" , "Scala" )
var string = score . mkString ( " , " )
println ( "Elements of Array are :\n" + string )
}
}
 Output:
 Explanation:
In the above code, we have declared an array of strings. We have used the mkString() method to convert the array of string to string and then print it using the print statement.

TOP Interview Coding Problems/Challenges

  • Run-length encoding (find/print frequency of letters in a string)

  • Sort an array of 0's, 1's and 2's in linear time complexity

  • Checking Anagrams (check whether two string is anagrams or not)

  • Relative sorting algorithm

  • Finding subarray with given sum

  • Find the level in a binary tree with given sum K

  • Check whether a Binary Tree is BST (Binary Search Tree) or not

  • 1[0]1 Pattern Count

  • Capitalize first and last letter of each word in a line

  • Print vertical sum of a binary tree

  • Print Boundary Sum of a Binary Tree

  • Reverse a single linked list

  • Greedy Strategy to solve major algorithm problems

  • Job sequencing problem

  • Root to leaf Path Sum

  • Exit Point in a Matrix

  • Find length of loop in a linked list

  • Toppers of Class

  • Print All Nodes that don't have Sibling

  • Transform to Sum Tree

  • Shortest Source to Destination Path



评论和讨论

广告:您是博主吗? 加入我们的Blogging论坛 。


请启用JavaScript以查看由Disqus提供的评论。


翻译自: https://www.includehelp.com/scala/how-to-print-an-array.aspx

scala中打印数组和集合

scala中打印数组和集合_如何在Scala中打印数组?相关推荐

  1. [转载] java中对数组进行排序_如何在Java中对数组排序

    参考链接: Java中main()的有效变体 java中对数组进行排序 Java Array is like a container that can hold a fixed number of t ...

  2. java中对数组进行排序_如何在Java中对数组排序

    java中对数组进行排序 Java Array is like a container that can hold a fixed number of the same type of items, ...

  3. redis中存集合_如何在Redis中管理集合

    redis中存集合 介绍 (Introduction) Redis is an open-source, in-memory key-value data store. Sets in Redis a ...

  4. python中glob模块怎么下_如何在Python中使用glob.glob模块搜索子文件夹?

    如何在Python中使用glob.glob模块搜索子文件夹? 我想在文件夹中打开一系列子文件夹,找到一些文本文件并打印一些文本文件行. 我用这个: configfiles = glob.glob('C ...

  5. word中 有注释标签吗_如何在Word中注释图像

    word中 有注释标签吗 If you're writing a document that includes images, you may want to add annotations to t ...

  6. python中none是什么类型_如何在Python中”测试”None类型?

    我有一个方法,它有时返回一个非类型的值.那么我怎样才能质疑一个非类型的变量呢?例如,我需要使用if方法 if not new: new = '#' 我知道这是错误的方式,我希望你理解我的意思. 我想这 ...

  7. cmd中加载java源文件_如何在cmd中编译和运行java源文件

    如何在cmd中编译和运行java源文件 首先写一个名为HelloWorld.java的java源文件,存储在如C:/java/src的地址,我们再假设待会要存储的位置是C:/java/bin,则我们做 ...

  8. java中md5加密和解密_如何在java中实现md5加密和解密

    如何在java中实现md5加密和解密 关注:273  答案:1  mip版 解决时间 2021-01-19 20:37 提问者精神疯裂 2021-01-19 05:36 如何在java中实现md5加密 ...

  9. mysql中用户权限的授予_如何在MySQL中创建用户和授予权限

    如何创建MySQL用户并授予权限?为了实现良好的安全性,需要为每个应用程序创建单独的用户帐户,而不是root用户访问数据库.这将确保应用程序无法访问其他应用程序的数据库.因此需要mysql管理员(ro ...

最新文章

  1. hdu-1166敌兵布阵
  2. 自学python找到工作-学完python能找到工作么
  3. oracle删除建分区索引,Oracle分区表之创建维护分区表索引的详细步骤
  4. SharePoint2013 备份还原
  5. C++学习之路 | PTA乙级—— 1017 A除以B (20分)(精简)
  6. 利用SiteMapPath控件做论坛导航
  7. 佳明或已支付勒索金,获得 WastedLocker的解密密钥
  8. 如何减少返工工作量?
  9. 高通Audio缩写(不断更新中...)
  10. 2.7 HDFS的使用
  11. 调试mysql语句_Mysql sql 语句调试
  12. centos php7.0 mysql,如何在centos7.3系统下安装php7.0
  13. 单片机 自动更改日期_自动螺丝机不可或缺之功能
  14. Java 阶乘(BigInteger)
  15. 【电场】基于模拟电荷法计算输电线路铁塔周围电场分布附matlab代码
  16. 全球(全国)土地利用数据集获取大全/植被类型分布/NDVI数据/NPP数据/径流量数据/降雨量气温气象数据/蒸散量数据/太阳辐射数据
  17. sqlserver 企业版下载地址
  18. 图片格式转换怎么做?教你几招搞定图片格式转换
  19. 张亮:十万级并发任务调度框架 ElasticJob 的定位与设计理念
  20. 【网络】之TCP/IP 网络模型有哪几层

热门文章

  1. GM65与stm32通信
  2. 社会工程学软件_信息工程学院大学生心理健康教育讲座
  3. JAVA毕业设计_机房管理系统
  4. 医院lis系统需要服务器吗,医院里的HIS系统和LIS系统有什么区别?
  5. 1022. Digital Library (30)
  6. nginx负载均衡配置,宕机自动切换方式
  7. PDPS软件:机器人控制旋转台运行虚拟仿真操作方法
  8. 安卓系统再曝高危漏洞!大部分手机都可能被黑客控制
  9. 【雕刻机】高质量双面PCB板的全过程
  10. 团队任务分配管理软件,可以分配团队任务的团队管理软件