How to fix the length of array in Scala
This article mainly explains "how to fix the length array in Scala". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how to fix the length array in Scala.
1. Fixed length array
How to create a fixed-length array
Use Array to define an array of constant length
Object ArrayDemo {
Def main (args: Array [String]) {
/ / initialize a fixed-length array with a length of 8 and all elements are 0
Val arr1 = new Array [Int] (8)
/ / directly print a fixed-length array containing the hashcode value of the array
Println (arr1)
/ / convert the array to an array buffer, and you can see the contents of the original array.
/ / toBuffer converts arrays to long array buffers
Println (arr1.toBuffer)
/ / Note: if you do not use new to get an array, it is equivalent to calling the apply method of the array and directly assigning values to the array
/ / initialize a fixed-length array with length 1 and value 10
Val arr2 = Array [Int] (10)
/ / output array element values
Println (arr2.toBuffer)
/ / define a fixed-length array of length 3
Val arr3 = Array ("hadoop", "storm", "spark")
/ / use () to access elements
Println (arr3 (2))
/ / an array of 10 integers with an initialization value of 0
Val nums = new Array [Int] (10)
/ / traversing the array
For (I)