Scala TreeSet copyToArray()方法及示例

Scala TreeSet copyToArray()方法及示例

Scala TreeSet类中, copyToArray() 方法用于将TreeSet的元素复制到数组中。

方法定义: def copyToArray[B >: A](xs: Array[B], start: Int, len: Int):Int

参数:

xs: 表示要复制元素的数组。

start: 表示复制的起始索引。默认值为0。

len: 表示要复制的元素的数量。其默认值是集合的长度。

返回类型。它将集合中的元素返回给一个数组。

例子 #1:

// Scala program of copyToArray() 
// method 
  
// Import TreeSet
import scala.collection.mutable._
  
// Creating object 
object GfG 
{ 
  
    // Main method 
    def main(args:Array[String]) 
    { 
      
        // Creating TreeSet
        val t1 = TreeSet(2, 1, 3, 4, 5) 
          
        // Print the TreeSet
        println(t1) 
          
        // Creating an array 
        val arr = Array(0, 0, 0, 0, 0)
          
        // Applying copyToArray() method  
        t1.copyToArray(arr)
          
        // Displays output 
        println("Elements in the array: ")
          
        for(elem <- arr)  
            print(elem + " ") 
          
    } 
} 

输出。

TreeSet(1, 2, 3, 4, 5)
Elements in the array: 
1 2 3 4 5

例子#2。

// Scala program of copyToArray() 
// method 
  
// Import TreeSet
import scala.collection.mutable._
  
// Creating object 
object GfG 
{ 
  
    // Main method 
    def main(args:Array[String]) 
    { 
      
        // Creating TreeSet
        val t1 = TreeSet(2, 1, 3, 4, 5) 
          
        // Print the TreeSet
        println(t1) 
          
        // Creating an array 
        val arr = Array(0, 0, 0, 0, 0)
          
        // Applying copyToArray() method  
        t1.copyToArray(arr, 1, 2)
          
        // Displays output 
        println("Elements in the array: ")
          
        for(elem <- arr)  
            print(elem + " ") 
          
    } 
} 

输出。

TreeSet(1, 2, 3, 4, 5)
Elements in the array: 
0 1 2 0 0

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程