{"id":220,"date":"2022-01-21T14:56:04","date_gmt":"2022-01-21T14:56:04","guid":{"rendered":"https:\/\/www.kindsonthegenius.com\/scala\/?p=220"},"modified":"2022-04-18T14:54:11","modified_gmt":"2022-04-18T14:54:11","slug":"scala-working-with-arrays","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/scala\/scala-working-with-arrays\/","title":{"rendered":"Scala &#8211; Working with Arrays"},"content":{"rendered":"<p>In this lesson, you will learn how to us arrays in Scala. Array in Scala works similar to arrays in Java, so let&#8217;s go ahead to get started!<\/p>\n<p>We&#8217;l cover the following sub-topics<\/p>\n<ol>\n<li><a href=\"#t1\">Declaring an Array<\/a><\/li>\n<li><a href=\"#t2\">Array Processing<\/a><\/li>\n<li><a href=\"#t3\">Multidimensional Arrays<\/a><\/li>\n<li><a href=\"#t4\">Array Concatenation<\/a><\/li>\n<li><a href=\"#t5\">Array With Range Function<\/a><\/li>\n<li><a href=\"#t6\">Some Useful Array Methods<\/a><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t1\">1. Declaring an Array<\/strong><\/h4>\n<p>As you know, before you can use any variable, you must declare it. Arrays are not exceptions. You can declare an array variable in Scala using the syntax below:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\">\/\/ Method 1<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">var<\/span> arr<span style=\"color: #008800; font-weight: bold;\">:<\/span><span style=\"color: #333399; font-weight: bold;\">Array<\/span><span style=\"color: #333333;\">[<\/span><span style=\"color: #333399; font-weight: bold;\">String<\/span><span style=\"color: #333333;\">]<\/span> <span style=\"color: #008800; font-weight: bold;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">new<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Array<\/span><span style=\"color: #333333;\">[<\/span><span style=\"color: #333399; font-weight: bold;\">String<\/span><span style=\"color: #333333;\">](<\/span><span style=\"color: #0000dd; font-weight: bold;\">3<\/span><span style=\"color: #333333;\">)<\/span>\r\n\r\n<span style=\"color: #888888;\">\/\/ Method 2<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">var<\/span> arr <span style=\"color: #008800; font-weight: bold;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">new<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Array<\/span><span style=\"color: #333333;\">[<\/span><span style=\"color: #333399; font-weight: bold;\">String<\/span><span style=\"color: #333333;\">](<\/span><span style=\"color: #0000dd; font-weight: bold;\">3<\/span><span style=\"color: #333333;\">)<\/span>\r\n<\/pre>\n<p>In these examples, we declare an array names arr, that can hold up to 3 String values.<\/p>\n<p>To access the elements of an array, you can use the index which is zero-based. Examples are given below:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">arr<span style=\"color: #333333;\">(<\/span><span style=\"color: #0000dd; font-weight: bold;\">0<\/span><span style=\"color: #333333;\">)<\/span> <span style=\"color: #008800; font-weight: bold;\">=<\/span> <span style=\"background-color: #fff0f0;\">\"Kindson\"<\/span><span style=\"color: #333333;\">;<\/span> \r\narr<span style=\"color: #333333;\">(<\/span><span style=\"color: #0000dd; font-weight: bold;\">1<\/span><span style=\"color: #333333;\">)<\/span> <span style=\"color: #008800; font-weight: bold;\">=<\/span> <span style=\"background-color: #fff0f0;\">\"Jadon\"<\/span><span style=\"color: #333333;\">;<\/span> \r\narr<span style=\"color: #333333;\">(<\/span><span style=\"color: #0000dd; font-weight: bold;\">2<\/span><span style=\"color: #333333;\">)<\/span> <span style=\"color: #008800; font-weight: bold;\">=<\/span> <span style=\"background-color: #fff0f0;\">\"Solace\"<\/span>\r\n\r\n<span style=\"color: #888888;\">\/\/Another method<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">var<\/span> arr <span style=\"color: #008800; font-weight: bold;\">=<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Array<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"Kindson\"<\/span><span style=\"color: #333333;\">,<\/span> <span style=\"background-color: #fff0f0;\">\"Jadon\"<\/span><span style=\"color: #333333;\">,<\/span> <span style=\"background-color: #fff0f0;\">\"Solace\"<\/span><span style=\"color: #333333;\">)<\/span>\r\n<\/pre>\n<p>So you can use either of the two methods<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t2\">2. Array Processing<\/strong><\/h4>\n<p>Normally we can process multiple array elements using a loop. In the example below we perform the following operations:<\/p>\n<ul>\n<li>Print all elements of the array<\/li>\n<li>Sum all elements of the array<\/li>\n<li>Find the maximum element in the array<\/li>\n<\/ul>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\">\/\/ Program to Demonstrate Array Processing<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">object<\/span> <span style=\"color: #bb0066; font-weight: bold;\">ArrayProcessing<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\r\n  <span style=\"color: #008800; font-weight: bold;\">def<\/span> main<span style=\"color: #333333;\">(<\/span>args<span style=\"color: #008800; font-weight: bold;\">:<\/span> <span style=\"color: #333399; font-weight: bold;\">Array<\/span><span style=\"color: #333333;\">[<\/span><span style=\"color: #333399; font-weight: bold;\">String<\/span><span style=\"color: #333333;\">])<\/span><span style=\"color: #008800; font-weight: bold;\">:<\/span> <span style=\"color: #333399; font-weight: bold;\">Unit<\/span> <span style=\"color: #333333;\">=<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\r\n    <span style=\"color: #008800; font-weight: bold;\">val<\/span> arr <span style=\"color: #008800; font-weight: bold;\">=<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Array<\/span><span style=\"color: #333333;\">(<\/span><span style=\"color: #0000dd; font-weight: bold;\">34<\/span><span style=\"color: #333333;\">,<\/span> <span style=\"color: #0000dd; font-weight: bold;\">66<\/span><span style=\"color: #333333;\">,<\/span> <span style=\"color: #0000dd; font-weight: bold;\">78<\/span><span style=\"color: #333333;\">,<\/span> <span style=\"color: #0000dd; font-weight: bold;\">3<\/span><span style=\"color: #333333;\">,<\/span> <span style=\"color: #0000dd; font-weight: bold;\">95<\/span><span style=\"color: #333333;\">)<\/span>\r\n\r\n    <span style=\"color: #888888;\">\/\/Print all element of the array<\/span>\r\n    <span style=\"color: #008800; font-weight: bold;\">for<\/span> <span style=\"color: #333333;\">(<\/span>ele <span style=\"color: #008800; font-weight: bold;\">&lt;-<\/span> arr<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n      println<span style=\"color: #333333;\">(<\/span>ele<span style=\"color: #333333;\">)<\/span>\r\n    <span style=\"color: #333333;\">}<\/span>\r\n\r\n    <span style=\"color: #888888;\">\/\/Sum all element of the array<\/span>\r\n    <span style=\"color: #008800; font-weight: bold;\">var<\/span> sum<span style=\"color: #008800; font-weight: bold;\">:<\/span> <span style=\"color: #333399; font-weight: bold;\">Int<\/span> <span style=\"color: #333333;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">0<\/span>\r\n    <span style=\"color: #008800; font-weight: bold;\">for<\/span> <span style=\"color: #333333;\">(<\/span>ele <span style=\"color: #008800; font-weight: bold;\">&lt;-<\/span> arr<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n      sum <span style=\"color: #008800; font-weight: bold;\">=<\/span> sum <span style=\"color: #333333;\">+<\/span> ele\r\n    <span style=\"color: #333333;\">}<\/span>\r\n    println<span style=\"color: #333333;\">(<\/span>s<span style=\"background-color: #fff0f0;\">\"The sume is $sum\"<\/span><span style=\"color: #333333;\">)<\/span>\r\n\r\n    <span style=\"color: #888888;\">\/\/Find the maximum element<\/span>\r\n    <span style=\"color: #008800; font-weight: bold;\">var<\/span> max <span style=\"color: #008800; font-weight: bold;\">=<\/span> arr<span style=\"color: #333333;\">(<\/span><span style=\"color: #0000dd; font-weight: bold;\">0<\/span><span style=\"color: #333333;\">)<\/span>\r\n    <span style=\"color: #008800; font-weight: bold;\">for<\/span> <span style=\"color: #333333;\">(<\/span>i <span style=\"color: #008800; font-weight: bold;\">&lt;-<\/span> <span style=\"color: #0000dd; font-weight: bold;\">1<\/span> to <span style=\"color: #333333;\">(<\/span>arr<span style=\"color: #333333;\">.<\/span>length <span style=\"color: #333333;\">-<\/span> <span style=\"color: #0000dd; font-weight: bold;\">1<\/span><span style=\"color: #333333;\">))<\/span> <span style=\"color: #333333;\">{<\/span>\r\n      <span style=\"color: #008800; font-weight: bold;\">if<\/span> <span style=\"color: #333333;\">(<\/span>arr<span style=\"color: #333333;\">(<\/span>i<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">&gt;<\/span> max<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n        max <span style=\"color: #008800; font-weight: bold;\">=<\/span> arr<span style=\"color: #333333;\">(<\/span>i<span style=\"color: #333333;\">)<\/span>\r\n      <span style=\"color: #333333;\">}<\/span>\r\n    <span style=\"color: #333333;\">}<\/span>\r\n    println<span style=\"color: #333333;\">(<\/span>s<span style=\"background-color: #fff0f0;\">\"The maximum number in the array is $max\"<\/span><span style=\"color: #333333;\">)<\/span>\r\n  <span style=\"color: #333333;\">}<\/span>\r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>The program above gives the following output<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">34\r\n66\r\n78\r\n3\r\n95\r\nThe sume is 276\r\nThe maximum number in the array is 95\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t3\">3. Multidimensional Arrays<\/strong><\/h4>\n<p>You can think of multidimensional array as an array of arrays. So we have the elements of the array to be arrays as well. That helps create a grid of table view of elements.<\/p>\n<p>The following syntax declares a 2-dimensional array called myGrid.<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">var<\/span> myGrid <span style=\"color: #008800; font-weight: bold;\">=<\/span> ofDim<span style=\"color: #333333;\">[<\/span><span style=\"color: #333399; font-weight: bold;\">Int<\/span><span style=\"color: #333333;\">](<\/span><span style=\"color: #0000dd; font-weight: bold;\">4<\/span><span style=\"color: #333333;\">,<\/span><span style=\"color: #0000dd; font-weight: bold;\">4<\/span><span style=\"color: #333333;\">)<\/span>\r\n<\/pre>\n<p>The code above creates an array\u00a0 of 4 elements each being an array of 4 elements<\/p>\n<p>The program below creates a 2-dimensional array using a nested loop and then prints out the elements using another nested loop<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\">\/\/ Program to Demonstrate Multidimensional Array Processing<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">object<\/span> <span style=\"color: #bb0066; font-weight: bold;\">MultidimensionalArray<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\r\n  <span style=\"color: #008800; font-weight: bold;\">def<\/span> main<span style=\"color: #333333;\">(<\/span>args<span style=\"color: #008800; font-weight: bold;\">:<\/span> <span style=\"color: #333399; font-weight: bold;\">Array<\/span><span style=\"color: #333333;\">[<\/span><span style=\"color: #333399; font-weight: bold;\">String<\/span><span style=\"color: #333333;\">])<\/span><span style=\"color: #008800; font-weight: bold;\">:<\/span> <span style=\"color: #333399; font-weight: bold;\">Unit<\/span> <span style=\"color: #333333;\">=<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\r\n    <span style=\"color: #008800; font-weight: bold;\">var<\/span> myGrid <span style=\"color: #008800; font-weight: bold;\">=<\/span> ofDim<span style=\"color: #333333;\">[<\/span><span style=\"color: #333399; font-weight: bold;\">Int<\/span><span style=\"color: #333333;\">](<\/span><span style=\"color: #0000dd; font-weight: bold;\">4<\/span><span style=\"color: #333333;\">,<\/span><span style=\"color: #0000dd; font-weight: bold;\">4<\/span><span style=\"color: #333333;\">)<\/span>\r\n\r\n    <span style=\"color: #888888;\">\/\/Assign items to grid<\/span>\r\n    <span style=\"color: #008800; font-weight: bold;\">for<\/span> <span style=\"color: #333333;\">(<\/span>i <span style=\"color: #008800; font-weight: bold;\">&lt;-<\/span> <span style=\"color: #0000dd; font-weight: bold;\">0<\/span> to <span style=\"color: #333333;\">(<\/span>myGrid<span style=\"color: #333333;\">.<\/span>length <span style=\"color: #333333;\">-<\/span><span style=\"color: #0000dd; font-weight: bold;\">1<\/span><span style=\"color: #333333;\">))<\/span> <span style=\"color: #333333;\">{<\/span>\r\n      <span style=\"color: #008800; font-weight: bold;\">for<\/span> <span style=\"color: #333333;\">(<\/span>j <span style=\"color: #008800; font-weight: bold;\">&lt;-<\/span> <span style=\"color: #0000dd; font-weight: bold;\">0<\/span> to <span style=\"color: #0000dd; font-weight: bold;\">3<\/span><span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n        myGrid<span style=\"color: #333333;\">(<\/span>i<span style=\"color: #333333;\">)(<\/span>j<span style=\"color: #333333;\">)<\/span> <span style=\"color: #008800; font-weight: bold;\">=<\/span> j\r\n      <span style=\"color: #333333;\">}<\/span>\r\n    <span style=\"color: #333333;\">}<\/span>\r\n\r\n    <span style=\"color: #888888;\">\/\/Print out grid elements<\/span>\r\n    <span style=\"color: #008800; font-weight: bold;\">for<\/span> <span style=\"color: #333333;\">(<\/span>i <span style=\"color: #008800; font-weight: bold;\">&lt;-<\/span> <span style=\"color: #0000dd; font-weight: bold;\">0<\/span> to <span style=\"color: #333333;\">(<\/span>myGrid<span style=\"color: #333333;\">.<\/span>length <span style=\"color: #333333;\">-<\/span><span style=\"color: #0000dd; font-weight: bold;\">1<\/span><span style=\"color: #333333;\">))<\/span> <span style=\"color: #333333;\">{<\/span>\r\n      <span style=\"color: #008800; font-weight: bold;\">for<\/span> <span style=\"color: #333333;\">(<\/span>j <span style=\"color: #008800; font-weight: bold;\">&lt;-<\/span> <span style=\"color: #0000dd; font-weight: bold;\">0<\/span> to <span style=\"color: #0000dd; font-weight: bold;\">3<\/span><span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n        print<span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"  \"<\/span> <span style=\"color: #333333;\">+<\/span> myGrid<span style=\"color: #333333;\">(<\/span>i<span style=\"color: #333333;\">)(<\/span>j<span style=\"color: #333333;\">))<\/span>\r\n      <span style=\"color: #333333;\">}<\/span>\r\n      println<span style=\"color: #333333;\">()<\/span>\r\n    <span style=\"color: #333333;\">}<\/span>\r\n\r\n  <span style=\"color: #333333;\">}<\/span>\r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t4\">4. Array Concatenation<\/strong><\/h4>\n<p>You can also concatenate two arrays using the concat() method. The concat() method for arrays take the two arrays to be concatenated. The syntax is:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">var<\/span> arr3<span style=\"color: #008800; font-weight: bold;\">:<\/span><span style=\"color: #333399; font-weight: bold;\">Array<\/span><span style=\"color: #333333;\">[<\/span><span style=\"color: #333399; font-weight: bold;\">String<\/span><span style=\"color: #333333;\">]<\/span> <span style=\"color: #008800; font-weight: bold;\">=<\/span> concat<span style=\"color: #333333;\">(<\/span>arr1<span style=\"color: #333333;\">,<\/span> arr2<span style=\"color: #333333;\">)<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>The program below illustrates\u00a0 concatenation of two arrays. Then printing out the elements of the concatenated array<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\">\/\/ Program to Demonstrate Multidimensional Array Processing<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">object<\/span> <span style=\"color: #bb0066; font-weight: bold;\">MultidimensionalArray<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\r\n  <span style=\"color: #008800; font-weight: bold;\">def<\/span> main<span style=\"color: #333333;\">(<\/span>args<span style=\"color: #008800; font-weight: bold;\">:<\/span> <span style=\"color: #333399; font-weight: bold;\">Array<\/span><span style=\"color: #333333;\">[<\/span><span style=\"color: #333399; font-weight: bold;\">String<\/span><span style=\"color: #333333;\">])<\/span><span style=\"color: #008800; font-weight: bold;\">:<\/span> <span style=\"color: #333399; font-weight: bold;\">Unit<\/span> <span style=\"color: #333333;\">=<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\r\n    <span style=\"color: #008800; font-weight: bold;\">var<\/span> arr1<span style=\"color: #008800; font-weight: bold;\">:<\/span><span style=\"color: #333399; font-weight: bold;\">Array<\/span><span style=\"color: #333333;\">[<\/span><span style=\"color: #333399; font-weight: bold;\">String<\/span><span style=\"color: #333333;\">]<\/span> <span style=\"color: #008800; font-weight: bold;\">=<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Array<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"Jadon\"<\/span><span style=\"color: #333333;\">,<\/span> <span style=\"background-color: #fff0f0;\">\"McMills\"<\/span><span style=\"color: #333333;\">,<\/span> <span style=\"background-color: #fff0f0;\">\"Treasure\"<\/span><span style=\"color: #333333;\">,<\/span> <span style=\"background-color: #fff0f0;\">\"Esther\"<\/span><span style=\"color: #333333;\">)<\/span>\r\n    <span style=\"color: #008800; font-weight: bold;\">var<\/span> arr2<span style=\"color: #008800; font-weight: bold;\">:<\/span><span style=\"color: #333399; font-weight: bold;\">Array<\/span><span style=\"color: #333333;\">[<\/span><span style=\"color: #333399; font-weight: bold;\">String<\/span><span style=\"color: #333333;\">]<\/span> <span style=\"color: #008800; font-weight: bold;\">=<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Array<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"Solace\"<\/span><span style=\"color: #333333;\">,<\/span> <span style=\"background-color: #fff0f0;\">\"Onyx\"<\/span><span style=\"color: #333333;\">,<\/span> <span style=\"background-color: #fff0f0;\">\"Trust\"<\/span><span style=\"color: #333333;\">,<\/span> <span style=\"background-color: #fff0f0;\">\"Praise\"<\/span><span style=\"color: #333333;\">)<\/span>\r\n    <span style=\"color: #008800; font-weight: bold;\">var<\/span> arr3<span style=\"color: #008800; font-weight: bold;\">:<\/span><span style=\"color: #333399; font-weight: bold;\">Array<\/span><span style=\"color: #333333;\">[<\/span><span style=\"color: #333399; font-weight: bold;\">String<\/span><span style=\"color: #333333;\">]<\/span> <span style=\"color: #008800; font-weight: bold;\">=<\/span> concat<span style=\"color: #333333;\">(<\/span>arr1<span style=\"color: #333333;\">,<\/span> arr2<span style=\"color: #333333;\">)<\/span>\r\n\r\n    <span style=\"color: #888888;\">\/\/print the concatenated array<\/span>\r\n    <span style=\"color: #008800; font-weight: bold;\">for<\/span> <span style=\"color: #333333;\">(<\/span>ele <span style=\"color: #008800; font-weight: bold;\">&lt;-<\/span> arr3<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n      println<span style=\"color: #333333;\">(<\/span>ele<span style=\"color: #333333;\">)<\/span>\r\n    <span style=\"color: #333333;\">}<\/span>\r\n\r\n  <span style=\"color: #333333;\">}<\/span>\r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t5\">5. Array With the Range Function<\/strong><\/h4>\n<p>You can use the range() function to generate a sequence of values between a given range. The range() function takes two arguments &#8211; start and end. It also takes an optional third argument, step which indicate then difference between subsequent values in the array.<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\">\/\/ Program to Demonstrate Array with Range<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">object<\/span> <span style=\"color: #bb0066; font-weight: bold;\">ArrayWithRange<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\r\n  <span style=\"color: #008800; font-weight: bold;\">def<\/span> main<span style=\"color: #333333;\">(<\/span>args<span style=\"color: #008800; font-weight: bold;\">:<\/span> <span style=\"color: #333399; font-weight: bold;\">Array<\/span><span style=\"color: #333333;\">[<\/span><span style=\"color: #333399; font-weight: bold;\">String<\/span><span style=\"color: #333333;\">])<\/span><span style=\"color: #008800; font-weight: bold;\">:<\/span> <span style=\"color: #333399; font-weight: bold;\">Unit<\/span> <span style=\"color: #333333;\">=<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\r\n    <span style=\"color: #008800; font-weight: bold;\">var<\/span> jumpTen<span style=\"color: #008800; font-weight: bold;\">:<\/span><span style=\"color: #333399; font-weight: bold;\">Array<\/span><span style=\"color: #333333;\">[<\/span><span style=\"color: #333399; font-weight: bold;\">Int<\/span><span style=\"color: #333333;\">]<\/span> <span style=\"color: #008800; font-weight: bold;\">=<\/span> range<span style=\"color: #333333;\">(<\/span><span style=\"color: #0000dd; font-weight: bold;\">10<\/span><span style=\"color: #333333;\">,<\/span> <span style=\"color: #0000dd; font-weight: bold;\">100<\/span><span style=\"color: #333333;\">,<\/span> <span style=\"color: #0000dd; font-weight: bold;\">10<\/span><span style=\"color: #333333;\">)<\/span>\r\n    <span style=\"color: #008800; font-weight: bold;\">var<\/span> jumpOne<span style=\"color: #008800; font-weight: bold;\">:<\/span><span style=\"color: #333399; font-weight: bold;\">Array<\/span><span style=\"color: #333333;\">[<\/span><span style=\"color: #333399; font-weight: bold;\">Int<\/span><span style=\"color: #333333;\">]<\/span> <span style=\"color: #008800; font-weight: bold;\">=<\/span> range<span style=\"color: #333333;\">(<\/span><span style=\"color: #0000dd; font-weight: bold;\">1<\/span><span style=\"color: #333333;\">,<\/span> <span style=\"color: #0000dd; font-weight: bold;\">10<\/span><span style=\"color: #333333;\">)<\/span>\r\n\r\n    <span style=\"color: #888888;\">\/\/print the  first array<\/span>\r\n    println<span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"Now Printing element of JumpTen...\"<\/span><span style=\"color: #333333;\">)<\/span>\r\n    <span style=\"color: #008800; font-weight: bold;\">for<\/span> <span style=\"color: #333333;\">(<\/span>ele <span style=\"color: #008800; font-weight: bold;\">&lt;-<\/span> jumpTen<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n      println<span style=\"color: #333333;\">(<\/span>ele<span style=\"color: #333333;\">)<\/span>\r\n    <span style=\"color: #333333;\">}<\/span>\r\n\r\n    <span style=\"color: #888888;\">\/\/print the  second array<\/span>\r\n    println<span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"Now Printing element of JumpOne...\"<\/span><span style=\"color: #333333;\">)<\/span>\r\n    <span style=\"color: #008800; font-weight: bold;\">for<\/span> <span style=\"color: #333333;\">(<\/span>ele <span style=\"color: #008800; font-weight: bold;\">&lt;-<\/span> jumpOne<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n      println<span style=\"color: #333333;\">(<\/span>ele<span style=\"color: #333333;\">)<\/span>\r\n    <span style=\"color: #333333;\">}<\/span>\r\n  <span style=\"color: #333333;\">}<\/span>\r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>I recommend you run the program yourself and see the output. Leave a comment below if you have challenges.<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t6\">6. Useful Array Methods<\/strong><\/h4>\n<p>The table below provides a list of some useful array methods.<\/p>\n<table class=\"table table-bordered\">\n<tbody>\n<tr>\n<th>SN<\/th>\n<th>Methods and Description<\/th>\n<\/tr>\n<tr>\n<td class=\"ts\">1<\/td>\n<td><b>def apply( x: T, xs: T* ): Array[T] &#8211; <\/b>Creates an array of T objects, where T can be any data type<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">2<\/td>\n<td><b>def concat[T]( xss: Array[T]* ): Array[T] &#8211; <\/b>Concatenates all arrays into one single array.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">3<\/td>\n<td><b>def copy( src: AnyRef, srcPos: Int, dest: AnyRef, destPos: Int, length: Int ): Unit &#8211;\u00a0<\/b>Copy one array to another. Equivalent to System.arraycopy(src, srcPos, dest, destPos, length) in Java<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">4<\/td>\n<td><b>def empty[T]: Array[T] &#8211;\u00a0<\/b>Returns an array of length 0<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">5<\/td>\n<td><b>def iterate[T]( start: T, len: Int )( f: (T) =&gt; T ): Array[T] &#8211;\u00a0<\/b>Returns an array containing repeated applications of a function to a start value.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">6<\/td>\n<td><b>def fill[T]( n: Int )(elem: =&gt; T): Array[T] &#8211;\u00a0<\/b>Returns an array that contains the results of some element computation a number of times.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">7<\/td>\n<td><b>def fill[T]( n1: Int, n2: Int )( elem: =&gt; T ): Array[Array[T]] &#8211; <\/b>Returns a two-dimensional array that contains the results of some element computation a number of times.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">8<\/td>\n<td><b>def iterate[T]( start: T, len: Int)( f: (T) =&gt; T ): Array[T] &#8211; <\/b>Returns an array containing repeated applications of a function to a start value.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">9<\/td>\n<td><b>def ofDim[T]( n1: Int ): Array[T] &#8211;\u00a0<\/b>Creates array with specified dimensions.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">10<\/td>\n<td><b>def ofDim[T]( n1: Int, n2: Int ): Array[Array[T]] &#8211;\u00a0<\/b>Creates a 2-dimensional array<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">11<\/td>\n<td><b>def ofDim[T]( n1: Int, n2: Int, n3: Int ): Array[Array[Array[T]]] &#8211;\u00a0<\/b>Creates a 3-dimensional array<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">12<\/td>\n<td><b>def range( start: Int, end: Int, step: Int ): Array[Int] &#8211;\u00a0<\/b>Returns an array that contains equally spaced values in some integer interval.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">13<\/td>\n<td><b>def range( start: Int, end: Int ): Array[Int] &#8211; <\/b>Returns an array containing a sequence of increasing integers in a range.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">14<\/td>\n<td><b>def tabulate[T]( n: Int )(f: (Int)=&gt; T): Array[T] &#8211; <\/b>Returns an array that contains values of a given function over a range of integer values starting from 0.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">15<\/td>\n<td><b>def tabulate[T]( n1: Int, n2: Int )( f: (Int, Int ) =&gt; T): Array[Array[T]] &#8211; <\/b>Returns a two-dimensional array that contains values of a given function over ranges of integer values starting from 0.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this lesson, you will learn how to us arrays in Scala. Array in Scala works similar to arrays in Java, so let&#8217;s go ahead &hellip; <\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[2],"tags":[36],"class_list":["post-220","post","type-post","status-publish","format-standard","hentry","category-scala-programming","tag-arrays"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.9 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Scala - Working with Arrays - Scala Programming<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/kindsonthegenius.com\/scala\/scala-working-with-arrays\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Scala - Working with Arrays - Scala Programming\" \/>\n<meta property=\"og:description\" content=\"In this lesson, you will learn how to us arrays in Scala. Array in Scala works similar to arrays in Java, so let&#8217;s go ahead &hellip;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/kindsonthegenius.com\/scala\/scala-working-with-arrays\/\" \/>\n<meta property=\"og:site_name\" content=\"Scala Programming\" \/>\n<meta property=\"article:published_time\" content=\"2022-01-21T14:56:04+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-04-18T14:54:11+00:00\" \/>\n<meta name=\"author\" content=\"kindsonthegenius\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"kindsonthegenius\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/scala\\\/scala-working-with-arrays\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/scala\\\/scala-working-with-arrays\\\/\"},\"author\":{\"name\":\"kindsonthegenius\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/scala\\\/#\\\/schema\\\/person\\\/d69f73eca381222e2124581298dff5b4\"},\"headline\":\"Scala &#8211; Working with Arrays\",\"datePublished\":\"2022-01-21T14:56:04+00:00\",\"dateModified\":\"2022-04-18T14:54:11+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/scala\\\/scala-working-with-arrays\\\/\"},\"wordCount\":741,\"commentCount\":0,\"keywords\":[\"Arrays\"],\"articleSection\":[\"Scala Programming\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/kindsonthegenius.com\\\/scala\\\/scala-working-with-arrays\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/scala\\\/scala-working-with-arrays\\\/\",\"url\":\"https:\\\/\\\/kindsonthegenius.com\\\/scala\\\/scala-working-with-arrays\\\/\",\"name\":\"Scala - Working with Arrays - Scala Programming\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/scala\\\/#website\"},\"datePublished\":\"2022-01-21T14:56:04+00:00\",\"dateModified\":\"2022-04-18T14:54:11+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/scala\\\/#\\\/schema\\\/person\\\/d69f73eca381222e2124581298dff5b4\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/scala\\\/scala-working-with-arrays\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/kindsonthegenius.com\\\/scala\\\/scala-working-with-arrays\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/scala\\\/scala-working-with-arrays\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/kindsonthegenius.com\\\/scala\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Scala &#8211; Working with Arrays\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/scala\\\/#website\",\"url\":\"https:\\\/\\\/kindsonthegenius.com\\\/scala\\\/\",\"name\":\"Scala Programming\",\"description\":\"Scala Programing Tutorials\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/kindsonthegenius.com\\\/scala\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/scala\\\/#\\\/schema\\\/person\\\/d69f73eca381222e2124581298dff5b4\",\"name\":\"kindsonthegenius\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b9d710de456c3d85e5614c3a6992fa3d527425e2ab32b8bd5d85bfbaa235004b?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b9d710de456c3d85e5614c3a6992fa3d527425e2ab32b8bd5d85bfbaa235004b?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b9d710de456c3d85e5614c3a6992fa3d527425e2ab32b8bd5d85bfbaa235004b?s=96&d=mm&r=g\",\"caption\":\"kindsonthegenius\"},\"url\":\"https:\\\/\\\/kindsonthegenius.com\\\/scala\\\/author\\\/kindsonthegenius-3\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Scala - Working with Arrays - Scala Programming","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/kindsonthegenius.com\/scala\/scala-working-with-arrays\/","og_locale":"en_US","og_type":"article","og_title":"Scala - Working with Arrays - Scala Programming","og_description":"In this lesson, you will learn how to us arrays in Scala. Array in Scala works similar to arrays in Java, so let&#8217;s go ahead &hellip;","og_url":"https:\/\/kindsonthegenius.com\/scala\/scala-working-with-arrays\/","og_site_name":"Scala Programming","article_published_time":"2022-01-21T14:56:04+00:00","article_modified_time":"2022-04-18T14:54:11+00:00","author":"kindsonthegenius","twitter_card":"summary_large_image","twitter_misc":{"Written by":"kindsonthegenius","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/kindsonthegenius.com\/scala\/scala-working-with-arrays\/#article","isPartOf":{"@id":"https:\/\/kindsonthegenius.com\/scala\/scala-working-with-arrays\/"},"author":{"name":"kindsonthegenius","@id":"https:\/\/kindsonthegenius.com\/scala\/#\/schema\/person\/d69f73eca381222e2124581298dff5b4"},"headline":"Scala &#8211; Working with Arrays","datePublished":"2022-01-21T14:56:04+00:00","dateModified":"2022-04-18T14:54:11+00:00","mainEntityOfPage":{"@id":"https:\/\/kindsonthegenius.com\/scala\/scala-working-with-arrays\/"},"wordCount":741,"commentCount":0,"keywords":["Arrays"],"articleSection":["Scala Programming"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/kindsonthegenius.com\/scala\/scala-working-with-arrays\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/kindsonthegenius.com\/scala\/scala-working-with-arrays\/","url":"https:\/\/kindsonthegenius.com\/scala\/scala-working-with-arrays\/","name":"Scala - Working with Arrays - Scala Programming","isPartOf":{"@id":"https:\/\/kindsonthegenius.com\/scala\/#website"},"datePublished":"2022-01-21T14:56:04+00:00","dateModified":"2022-04-18T14:54:11+00:00","author":{"@id":"https:\/\/kindsonthegenius.com\/scala\/#\/schema\/person\/d69f73eca381222e2124581298dff5b4"},"breadcrumb":{"@id":"https:\/\/kindsonthegenius.com\/scala\/scala-working-with-arrays\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/kindsonthegenius.com\/scala\/scala-working-with-arrays\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/kindsonthegenius.com\/scala\/scala-working-with-arrays\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/kindsonthegenius.com\/scala\/"},{"@type":"ListItem","position":2,"name":"Scala &#8211; Working with Arrays"}]},{"@type":"WebSite","@id":"https:\/\/kindsonthegenius.com\/scala\/#website","url":"https:\/\/kindsonthegenius.com\/scala\/","name":"Scala Programming","description":"Scala Programing Tutorials","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/kindsonthegenius.com\/scala\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/kindsonthegenius.com\/scala\/#\/schema\/person\/d69f73eca381222e2124581298dff5b4","name":"kindsonthegenius","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/b9d710de456c3d85e5614c3a6992fa3d527425e2ab32b8bd5d85bfbaa235004b?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/b9d710de456c3d85e5614c3a6992fa3d527425e2ab32b8bd5d85bfbaa235004b?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/b9d710de456c3d85e5614c3a6992fa3d527425e2ab32b8bd5d85bfbaa235004b?s=96&d=mm&r=g","caption":"kindsonthegenius"},"url":"https:\/\/kindsonthegenius.com\/scala\/author\/kindsonthegenius-3\/"}]}},"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/posts\/220","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/comments?post=220"}],"version-history":[{"count":3,"href":"https:\/\/kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/posts\/220\/revisions"}],"predecessor-version":[{"id":253,"href":"https:\/\/kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/posts\/220\/revisions\/253"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/media?parent=220"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/categories?post=220"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/tags?post=220"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}