Q:

Scala program to create the clone of an array

belongs to collection: Scala Array Programs

0

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.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program/Source Code:

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.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Scala Array Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Scala program to demonstrate the copyToArray() met... >>
<< Scala program to demonstrate the 3D array...