{"id":5707,"date":"2020-10-08T05:13:11","date_gmt":"2020-10-08T05:13:11","guid":{"rendered":"https:\/\/prwatech.in\/blog\/?p=5707"},"modified":"2024-04-12T09:26:39","modified_gmt":"2024-04-12T09:26:39","slug":"array-buffer-in-scala-collections-arraybuffer","status":"publish","type":"post","link":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/array-buffer-in-scala-collections-arraybuffer\/","title":{"rendered":"Scala &#8211; Array Buffer"},"content":{"rendered":"\r\n<h2><span data-sheets-root=\"1\" data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;Scala Collections - ArrayBuffer&quot;}\" data-sheets-userformat=\"{&quot;2&quot;:6145,&quot;3&quot;:{&quot;1&quot;:0,&quot;3&quot;:1},&quot;14&quot;:{&quot;1&quot;:3,&quot;3&quot;:1},&quot;15&quot;:&quot;Arial&quot;}\">Scala Collections &#8211; ArrayBuffer<\/span><\/h2>\r\n<p>&nbsp;<\/p>\r\n<p><strong>Array<\/strong> in <strong><a href=\"https:\/\/prwatech.in\/blog\/scala\/scala-a-quick-overview\/\">Scala <\/a><\/strong>is homogeneous and <a href=\"https:\/\/prwatech.in\/blog\/#\">alterable<\/a>, i.e it <a href=\"https:\/\/www.scala-lang.org\/\">contains<\/a> components of a similar data type and its components can change however the size of array can&#8217;t change. To make an mutable, indexed sequence whose size can change <strong>ArrayBuffer<\/strong> class is utilized. To utilize, <strong>ArrayBuffer<\/strong>, <em><strong>scala.collection.mutable.ArrayBuffer<\/strong><\/em> class is imported, a instance of ArrayBuffer is created.<\/p>\r\n\r\n\r\n\r\n<p>An <strong>ArrayBuffer<\/strong> is an Array of components, as well as the store&#8217;s present size of the array. At the point when a element is added to an ArrayBuffer, this size is checked. if suppose the underlying array isn&#8217;t full, the element is straightforwardly added to the array. and if the underlying array is full then a another large array is created and all the elements are copied into it.<\/p>\r\n<p>An <code>ArrayBuffer<\/code> in Scala is a mutable, resizable collection that provides efficient sequential access to elements. It is part of the mutable sequence collections in Scala and is implemented using an array internally. Unlike fixed-size arrays, an <code>ArrayBuffer<\/code> can grow or shrink dynamically, allowing for flexible manipulation of elements without needing to predefine the size.<\/p>\r\n<p>To use an <code>ArrayBuffer<\/code>, you first import <code>scala.collection.mutable.ArrayBuffer<\/code> and then instantiate it with elements. You can add elements to the end of an <code>ArrayBuffer<\/code> using the <code>+=<\/code> operator or append multiple elements with the <code>++=<\/code> operator. Elements can also be removed using the <code>-=<\/code>, <code>--=<\/code>, or <code>remove<\/code> methods.<\/p>\r\n\r\n\r\n\r\n<p><strong>Example:<\/strong><\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-preformatted\">var name = new ArrayBuffer[datatype]()  \/\/ empty buffer is created\r\n\r\nvar name = new ArrayBuffer(\"Mark\", \"prwa\", \"prwatech\")\r\n\r\n<\/pre>\r\n\r\n\r\n\r\n<p><strong>Use case 1:<\/strong><\/p>\r\n\r\n\r\n\r\n<p><strong>Creating instance of ArrayBuffer:<\/strong><\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>import scala.collection.mutable.ArrayBuffer\r\nobject prwatech {\r\n  def main(args: Array[String])\r\n{\r\n    \/\/ Instance of ArrayBuffer is created\r\n    var name = ArrayBuffer[String]() \r\n    name += \"Prwatech\"\r\n    name += \"Data Science\"\r\n    name += \"Big Data\"\r\n    println(name)\r\n}\r\n}\r\n\r\n\r\nOutput \r\nArrayBuffer(Prwatech, Data Science, Big Data)\r\n\r\n<\/code><\/pre>\r\n\r\n\r\n\r\n<p><strong>Use case 2:<\/strong><\/p>\r\n\r\n\r\n\r\n<p><strong>Access element from ArrayBuffer<\/strong><\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>\/\/ Scala program to access element of ArrayBuffer\r\n\r\nimport scala.collection.mutable.ArrayBuffer\r\n\/\/ Creating Object\r\nobject GFG\r\n{\r\n\/\/ Main Method\r\ndef main(args: Array[String])\r\n{\r\n\/\/ Instance of ArrayBuffer is created\r\nvar name = ArrayBuffer[String]()\r\nname += \"Prwatech\"\r\nname += \"Data Science\"\r\nname += \"Big Data\"\r\n\r\n\/\/ accessing 1th index element of arraybuffer\r\nprintln(name(1)) \r\n}\r\n}\r\n\r\nOutput:\r\nData Science<\/code><\/pre>\r\n\r\n\r\n\r\n<p><strong>Use case 3:<\/strong><\/p>\r\n\r\n\r\n\r\n<p><strong>Deleting ArrayBuffer Elements:<\/strong><\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>import scala.collection.mutable.ArrayBuffer\r\n\/\/ Creating Object\r\nobject Prwatech \r\n\/\/ Main Method\r\ndef main(args: Array[String])\r\n{\r\n    \/\/ Instance of ArrayBuffer is created\r\n    var name = ArrayBuffer( \"Prwatech\",\"Data Science\",\"Big \r\ndata\",\"Machine Learning\",\"Linux\",\"MySql\" ) \r\n   \/\/ deletes one element\r\n   name -= \"Prwatech\"     \r\n    \r\n   \/\/ deletes two or more elements \r\n   name -= (\"Linux\", \"Machine Learning\")\r\n\r\n   \/\/ printing resultant arraybuffer\r\n   println(name) \r\n }\r\n}\r\n\r\nOutput:\r\nArrayBuffer(Data Science, Big data, MySql)\r\n<\/code><\/pre>\r\n\r\n\r\n\r\n<p>Scala Collections &#8211; ArrayBuffer<\/p>\r\n","protected":false},"excerpt":{"rendered":"<p>Scala Collections &#8211; ArrayBuffer &nbsp; Array in Scala is homogeneous and alterable, i.e it contains components of a similar data type and its components can change however the size of array can&#8217;t change. To make an mutable, indexed sequence whose size can change ArrayBuffer class is utilized. To utilize, ArrayBuffer, scala.collection.mutable.ArrayBuffer class is imported, a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[565,1698],"tags":[1274,1280,1278,1277,1275,1276,1005,1282,1281,1279],"class_list":["post-5707","post","type-post","status-publish","format-standard","hentry","category-scala","category-scala-modules-scala","tag-array-buffer","tag-array-buffer-examples-in-scala","tag-array-buffer-in-scala","tag-array-buffer-use-cases","tag-define-array-buffer","tag-explain-array-buffer","tag-scala-array-buffer","tag-scala-array-buffer-access-lement","tag-scala-array-buffer-delete-elements","tag-what-is-array-buffer"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Scala Collections - ArrayBuffer<\/title>\n<meta name=\"description\" content=\"Master Scala Collections: ArrayBuffer - Dive deep with our expert instructors and comprehensive curriculum.\" \/>\n<meta name=\"robots\" content=\"noindex, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Scala Collections - ArrayBuffer\" \/>\n<meta property=\"og:description\" content=\"Master Scala Collections: ArrayBuffer - Dive deep with our expert instructors and comprehensive curriculum.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/array-buffer-in-scala-collections-arraybuffer\/\" \/>\n<meta property=\"og:site_name\" content=\"Prwatech\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/prwatech.in\/\" \/>\n<meta property=\"article:published_time\" content=\"2020-10-08T05:13:11+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-04-12T09:26:39+00:00\" \/>\n<meta name=\"author\" content=\"Prwatech\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@Eduprwatech\" \/>\n<meta name=\"twitter:site\" content=\"@Eduprwatech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Prwatech\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/array-buffer-in-scala-collections-arraybuffer\/\",\"url\":\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/array-buffer-in-scala-collections-arraybuffer\/\",\"name\":\"Scala Collections - ArrayBuffer\",\"isPartOf\":{\"@id\":\"https:\/\/prwatech.in\/blog\/#website\"},\"datePublished\":\"2020-10-08T05:13:11+00:00\",\"dateModified\":\"2024-04-12T09:26:39+00:00\",\"author\":{\"@id\":\"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3\"},\"description\":\"Master Scala Collections: ArrayBuffer - Dive deep with our expert instructors and comprehensive curriculum.\",\"breadcrumb\":{\"@id\":\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/array-buffer-in-scala-collections-arraybuffer\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/array-buffer-in-scala-collections-arraybuffer\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/array-buffer-in-scala-collections-arraybuffer\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/prwatech.in\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Scala &#8211; Array Buffer\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/prwatech.in\/blog\/#website\",\"url\":\"https:\/\/prwatech.in\/blog\/\",\"name\":\"Prwatech\",\"description\":\"Share Ideas, Start Something Good.\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/prwatech.in\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3\",\"name\":\"Prwatech\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prwatech.in\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/c00bafc1b04045f31eda917de39891456c44fa47c092b9bb6be0f860a3a30a2f?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/c00bafc1b04045f31eda917de39891456c44fa47c092b9bb6be0f860a3a30a2f?s=96&d=mm&r=g\",\"caption\":\"Prwatech\"},\"url\":\"https:\/\/prwatech.in\/blog\/author\/prwatech123\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Scala Collections - ArrayBuffer","description":"Master Scala Collections: ArrayBuffer - Dive deep with our expert instructors and comprehensive curriculum.","robots":{"index":"noindex","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"og_locale":"en_US","og_type":"article","og_title":"Scala Collections - ArrayBuffer","og_description":"Master Scala Collections: ArrayBuffer - Dive deep with our expert instructors and comprehensive curriculum.","og_url":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/array-buffer-in-scala-collections-arraybuffer\/","og_site_name":"Prwatech","article_publisher":"https:\/\/www.facebook.com\/prwatech.in\/","article_published_time":"2020-10-08T05:13:11+00:00","article_modified_time":"2024-04-12T09:26:39+00:00","author":"Prwatech","twitter_card":"summary_large_image","twitter_creator":"@Eduprwatech","twitter_site":"@Eduprwatech","twitter_misc":{"Written by":"Prwatech","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/array-buffer-in-scala-collections-arraybuffer\/","url":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/array-buffer-in-scala-collections-arraybuffer\/","name":"Scala Collections - ArrayBuffer","isPartOf":{"@id":"https:\/\/prwatech.in\/blog\/#website"},"datePublished":"2020-10-08T05:13:11+00:00","dateModified":"2024-04-12T09:26:39+00:00","author":{"@id":"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3"},"description":"Master Scala Collections: ArrayBuffer - Dive deep with our expert instructors and comprehensive curriculum.","breadcrumb":{"@id":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/array-buffer-in-scala-collections-arraybuffer\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/array-buffer-in-scala-collections-arraybuffer\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/array-buffer-in-scala-collections-arraybuffer\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/prwatech.in\/blog\/"},{"@type":"ListItem","position":2,"name":"Scala &#8211; Array Buffer"}]},{"@type":"WebSite","@id":"https:\/\/prwatech.in\/blog\/#website","url":"https:\/\/prwatech.in\/blog\/","name":"Prwatech","description":"Share Ideas, Start Something Good.","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/prwatech.in\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3","name":"Prwatech","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prwatech.in\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/c00bafc1b04045f31eda917de39891456c44fa47c092b9bb6be0f860a3a30a2f?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c00bafc1b04045f31eda917de39891456c44fa47c092b9bb6be0f860a3a30a2f?s=96&d=mm&r=g","caption":"Prwatech"},"url":"https:\/\/prwatech.in\/blog\/author\/prwatech123\/"}]}},"_links":{"self":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/5707","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/comments?post=5707"}],"version-history":[{"count":7,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/5707\/revisions"}],"predecessor-version":[{"id":11368,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/5707\/revisions\/11368"}],"wp:attachment":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/media?parent=5707"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/categories?post=5707"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/tags?post=5707"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}