{"id":3108,"date":"2016-02-09T02:48:27","date_gmt":"2016-02-09T02:48:27","guid":{"rendered":"http:\/\/www.garysieling.com\/blog\/?p=3108"},"modified":"2016-02-09T02:48:27","modified_gmt":"2016-02-09T02:48:27","slug":"scala-array-indexing","status":"publish","type":"post","link":"https:\/\/www.garysieling.com\/blog\/scala-array-indexing\/","title":{"rendered":"Scala array indexing"},"content":{"rendered":"<p>Scala arrays are 0-based and are accessed with parenthesis, rather than square brackets:<\/p>\n<pre lang=\"scala\">\nval x = Array(1,2,3)\n\nx(0)\n<\/pre>\n<p>You can also index them inline, or use the same syntax to instantiate other collections:<\/p>\n<pre lang=\"scala\">\nscala> List(1,2,3)(0)\nres10: Int = 1\n\nscala> List(1,2,3)(1)\nres11: Int = 2\n<\/pre>\n<p>Unlike languages like Python and R, collections will not wrap around:<\/p>\n<pre lang=\"scala\">\nscala> List(1,2,3)(3)\njava.lang.IndexOutOfBoundsException: 3\n  at scala.collection.LinearSeqOptimized$class.apply(LinearSeqOptimized.scala:51)\n  at scala.collection.immutable.List.apply(List.scala:83)\n  ... 33 elided\n\nscala> List(1,2,3)(-1)\njava.lang.IndexOutOfBoundsException: -1\n  at scala.collection.LinearSeqOptimized$class.apply(LinearSeqOptimized.scala:51)\n  at scala.collection.immutable.List.apply(List.scala:83)\n  ... 33 elided\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Scala arrays are 0-based and are accessed with parenthesis, rather than square brackets: val x = Array(1,2,3) x(0) You can also index them inline, or use the same syntax to instantiate other collections: scala> List(1,2,3)(0) res10: Int = 1 scala> List(1,2,3)(1) res11: Int = 2 Unlike languages like Python and R, collections will not wrap &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.garysieling.com\/blog\/scala-array-indexing\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Scala array indexing&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[4],"tags":[60,480,481,482,485],"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/3108"}],"collection":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/comments?post=3108"}],"version-history":[{"count":0,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/3108\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/media?parent=3108"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/categories?post=3108"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/tags?post=3108"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}