{"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:\/\/www.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":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[36],"class_list":["post-220","post","type-post","status-publish","format-standard","hentry","category-scala-programming","tag-arrays"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/posts\/220","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/comments?post=220"}],"version-history":[{"count":3,"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/posts\/220\/revisions"}],"predecessor-version":[{"id":253,"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/posts\/220\/revisions\/253"}],"wp:attachment":[{"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/media?parent=220"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/categories?post=220"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/tags?post=220"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}