Here, we will create an array of integer elements. Then we will use the size() method to get the size of the array and print the result on the console screen.
The source code to print the size of the array is given below. The given program is compiled and executed on the ubuntu 18.04 operating system successfully.
// Scala program to print the
// size of the array
object Sample {
def main(args: Array[String]) {
var arr = Array(1, 2, 3, 4, 5);
printf("Size of array: %d\n", arr.size);
}
}
Output:
Size of array: 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 arr that contains 5 integer elements. Then we used the size() method to get the size of an array. After that, we printed the result on the console screen.
Program/Source Code:
The source code to print the size of the array is given below. The given program is compiled and executed on the ubuntu 18.04 operating system successfully.
Output:
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 arr that contains 5 integer elements. Then we used the size() method to get the size of an array. After that, we printed the result on the console screen.
need an explanation for this answer? contact us directly to get an explanation for this answer