scala中打印数组和集合_如何在Scala中打印数组?

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.





Comments and Discussions

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


    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.





评论和讨论

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


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

scala中打印数组和集合