×

Scala Tutorial

Scala Basics

Scala Control statements

Scala Functions

Scala Arrays

Scala Lists

Scala Strings

Scala Classes & Objects

Scala Collections

BitSet copyToArray() Method in Scala

By IncludeHelp Last updated : November 14, 2024

BitSet in Scala is a special collection of positive integers. Scala programming language has a huge library containing a lot of utility functions to help working with data structure easy.

BitSet copyToArray() method

BitSet copyToArray() method is used to copy elements of BitSet to an array. It takes an array and copies the element of the BitSet to the array.

Syntax

BitSet_Name.copyToArray(array_Name)

Parameters

It accepts a single parameter which is the array in which the elements of BitSet are stored.

Return Type

It does not return any parameter.

Example 1

Program to illustrate the working of copyToArray() method

// Program to illustrate the working of copyToArray() method

import scala.collection.immutable.BitSet

object MyObject {
    def main(args: Array[String]): Unit = {
        val myBitSet = BitSet(4, 1, 6, 2, 9)
        println("myBitSet : " + myBitSet)
        
        var myArray = new Array[Int]( myBitSet.count(x => true))
        myBitSet.copyToArray(myArray)
        print("The array is ")
        for (i <- 0 to (myArray.length - 1) )
            print(myArray(i) + " ")
    }
}

Output

myBitSet : BitSet(1, 2, 4, 6, 9)
The array is 1 2 4 6 9 

Explanation

In the above code, we have created a BitSet named myBitSet with some elements and printed it. Then we have created an array of same size and using the copyToArray() to copy elements to the array. And then printed it.

Example 2

Program to illustrate the working of copyToArray() method

// Program to illustrate the working of copyToArray() method

import scala.collection.immutable.BitSet

object MyObject {
    def main(args: Array[String]): Unit = {
        val myBitSet = BitSet(4, 1, 6, 2, 9)
        println("myBitSet : " + myBitSet)
        
        val myArray: Array[Int] = Array(0, 0, 0, 0, 0, 0, 0, 0)
        myBitSet.copyToArray(myArray)
    
        print("The array is ")
        for (i <- 0 to (myArray.length - 1) )
            print(myArray(i) + " ")
    }
}

Output

myBitSet : BitSet(1, 2, 4, 6, 9)
The array is 1 2 4 6 9 0 0 0 

Explanation

In the above code, we have created a BitSet named myBitSet and printed it. Then we have created an array called myArray with 8 0's. Then we have used the copyToArray() method to copy elements of myBitSet to myArray and printed the resulting array which has all elements of the BitSet and trailing 0's.

Example 3

Program to illustrate the working of copyToArray() method

// Program to illustrate the working of copyToArray() method

import scala.collection.immutable.BitSet

object MyObject {
    def main(args: Array[String]): Unit = {
        val myBitSet = BitSet(4, 1, 6, 2, 9)
        println("myBitSet : " + myBitSet)
        
        val myArray: Array[Int] = Array(0, 0, 0)
    
        myBitSet.copyToArray(myArray)
    
        print("The array is ")
        for (i <- 0 to (myArray.length - 1) )
            print(myArray(i) + " ")
    }
}

Output

myBitSet : BitSet(1, 2, 4, 6, 9)
The array is 1 2 4 

Explanation

In the above code, we have created a BitSet named myBitSet and printed it. Then we have created an array called myArray with 3 0's. Then we have used the copyToArray() method to copy elements of myBitSet to myArray and printed the resulting array which has elements of the BitSet upto index 3.

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.