Scala program to create the clone of an array

Here, we are going to learn how to create the clone of an array in Scala programming language?
Submitted by Nidhi, on May 20, 2021 [Last updated : March 10, 2023]

Scala – Cloning of an Array

Here, we will create an array of integer elements. Then we will create the clone of the array using the clone() method. After that, we will both arrays on the console screen.

Scala code to create the clone of an array

The source code to create the clone of an array is given below. The given program is compiled and executed on the ubuntu 18.04 operating system successfully.

// Scala program to create the clone of an array

object Sample {
  def main(args: Array[String]) {
    var arr = Array(1, 2, 3, 4, 5)
    var cloneArr = arr.clone()

    var i: Int = 0;

    printf("Elements of Array 'arr':\n")
    i = 0;
    while (i < 5) {
      printf("%d ", arr(i));
      i = i + 1;
    }
    println();

    printf("Elements of Array 'cloneArr':\n")
    i = 0;
    while (i < 5) {
      printf("%d ", cloneArr(i));
      i = i + 1;
    }
    println();
  }
}

Output

Elements of Array 'arr':
1 2 3 4 5 
Elements of Array 'cloneArr':
1 2 3 4 5

Explanation

In the above program, we used an object-oriented approach to create the program. We created an object Sample, and we defined main() function. The main() function is the entry point for the program.

In the main() function, we created an array of integers. Then we used the clone() method to create the clone of the created array. After that, we printed both arrays on the console screen.

Scala Array Programs »



Related Programs



All Scala Programs

Comments and Discussions!

Load comments ↻





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