ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

BitSet copyToArray() Method in Scala with Example

Here, we will learn about copyToArray() method defined on BitSet in Scala. It is used to copy elements of the BitSet to an array. We will learn about count method in Scala with syntax and examples.
Submitted by Shivang Yadav, on December 08, 2020

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]) {
        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.

Program 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]) {
        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.

Program 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]) {
        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.

ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.