YouTip LogoYouTip

Scala Arrays

Arrays provided in the Scala language are used to store fixed-size elements of the same type. Arrays are one of the most important data structures in every programming language.\n\nDeclaring an array variable does not mean declaring separate variables like number0, number1, ..., number99, but declaring a variable like numbers, and then using numbers, numbers, ..., numbers to represent individual variables. A specified element in an array is accessed by its index.\n\nThe index of the first element in the array is 0, and the index of the last element is the total number of elements minus 1.\n\n* * *\n\n## Declaring Arrays\n\nThe following is the syntax for declaring an array in Scala:\n\nvar z:Array = new Array(3)orvar z = new Array(3)\nIn the above syntax, z declares an array of type String with a length of 3, which can store 3 elements. We can set values for each element and access each element by index, as shown below:\n\nz(0) = "Tutorial"; z(1) = "Baidu"; z(4/2) = "Google"\nThe index of the last element uses the expression **4/2** as the index, which is similar to **z(2) = "Google"**.\n\nWe can also define an array using the following method:\n\nvar z = Array("Tutorial", "Baidu", "Google")\nThe following image shows an array named myList with a length of 10, with index values from 0 to 9:\n\n!(#)\n\n* * *\n\n## Processing Arrays\n\nThe element type and the size of the array are both fixed, so when processing array elements, we usually use a basic for loop.\n\nThe following example demonstrates the creation and initialization of an array:\n\n## Example\n\nobject Test {\n\ndef main(args: Array){\n\nvar myList = Array(1.9, 2.9, 3.4, 3.5)\n\n// Output all array elements\n\nfor( x <- myList ){\n\nprintln( x )\n\n}\n\n// Calculate the sum of all array elements\n\nvar total =0.0;\n\nfor( i <- 0 to (myList.length - 1)){\n\ntotal += myList(i);\n\n}\n\n println("The sum is " + total);\n\n// Find the maximum element in the array\n\nvar max = myList(0);\n\nfor( i max) max = myList(i);\n\n}\n\n println("The maximum value is " + max);\n\n}\n\n}\n\nExecute the above code, the output result is:\n\n$ scalac Test.scala $ scala Test1.92.93.43.5The sum is 11.7The maximum value is 3.5\n\n* * *\n\n## Multi-dimensional Arrays\n\nIn a multi-dimensional array, a value in one array can be another array, and a value in that another array can also be an array. Matrices and tables are common two-dimensional arrays.\n\nThe following is an example of defining a two-dimensional array:\n\nval myMatrix = Array.ofDim(3, 3)\nIn the example, the array contains three array elements, and each array element in turn contains three values.\n\nNext, let's look at a complete example of processing a two-dimensional array:\n\n## Example\n\nimport Array._\n\nobject Test {\n\ndef main(args: Array){\n\nval myMatrix = Array.ofDim(3, 3)\n\n// Create matrix\n\nfor(i <- 0 to 2){\n\nfor( j <- 0 to 2){\n\n myMatrix(i)(j)= j;\n\n}\n\n}\n\n// Print two-dimensional array\n\nfor(i <- 0 to 2){\n\nfor( j <- 0 to 2){\n\n print(" " + myMatrix(i)(j));\n\n}\n\nprintln();\n\n}\n\n}\n\n}\n\nExecute the above code, the output result is:\n\n$ scalac Test.scala $ scala Test0 1 20 1 20 1 2\n\n* * *\n\n## Merging Arrays\n\nIn the following example, we use the concat() method to merge two arrays. The concat() method accepts multiple array parameters:\n\n## Example\n\nimport Array._\n\nobject Test {\n\ndef main(args: Array){\n\nvar myList1 = Array(1.9, 2.9, 3.4, 3.5)\n\nvar myList2 = Array(8.9, 7.9, 0.4, 1.5)\n\nvar myList3 =concat( myList1, myList2)\n\n// Output all array elements\n\nfor( x <- myList3 ){\n\nprintln( x )\n\n}\n\n}\n\n}\n\nExecute the above code, the output result is:\n\n$ scalac Test.scala $ scala Test1.92.93.43.58.97.90.41.5\n\n* * *\n\n## Creating Range Arrays\n\nIn the following example, we use the range() method to generate an array within a range. The last parameter of the range() method is the step, which defaults to 1:\n\n## Example\n\nimport Array._\n\nobject Test {\n\ndef main(args: Array){\n\nvar myList1 = range(10, 20, 2)\n\nvar myList2 = range(10,20)\n\n// Output all array elements\n\nfor( x <- myList1 ){\n\nprint(" " + x )\n\n}\n\n println()\n\nfor( x T ): Array** Returns an array of the specified length, where each element is the return value of the specified function. In the above example, the initial value of the array is 0, the length is 3, and the calculation function is **a=>a+1**: scala> Array.iterate(0,3)(a=>a+1) res1: Array = Array(0, 1, 2) |\n| 6 | **def fill( n: Int )(elem: => T): Array** Returns an array with the length specified by the first parameter, and each element is filled using the second parameter. |\n| 7 | **def fill( n1: Int, n2: Int )( elem: => T ): Array[Array]** Returns a two-dimensional array with the length specified by the first parameter, and each element is filled using the second parameter. |\n| 8 | **def ofDim( n1: Int ): Array** Creates an array of the specified length |\n| 9 | **def ofDim( n1: Int, n2: Int ): Array[Array]** Creates a two-dimensional array |\n| 10 | **def ofDim( n1: Int, n2: Int, n3: Int ): Array[Array[Array]]** Creates a three-dimensional array |\n| 11 | **def range( start: Int, end: Int, step: Int ): Array** Creates an array within the specified range, where step is the step between each element |\n| 12 | **def range( start: Int, end: Int ): Array** Creates an array within the specified range |\n| 13 | **def tabulate( n: Int )(f: (Int)=> T): Array** Returns an array of the specified length, where each element is the return value of the specified function, starting from 0 by default. The above example returns 3 elements: scala> Array.tabulate(3)(a => a + 5) res0: Array = Array(5, 6, 7) |\n| 14 | **def tabulate( n1: Int, n2: Int )( f: (Int, Int ) => T): Array[Array]** Returns a two-dimensional array of the specified length, where each element is the return value of the specified function, starting from 0 by default. |
← Scala CollectionsScala Strings β†’