HomeSearch

Scala 2D List, Array Examples: Array.ofDim

Create a 2D list by nesting lists within a list. Call Array.ofDim for a 2D array.
2D list. In our universe a flat surface has two dimensions. We move north, east, south, west. It is a grid. In Scala we can represent this surface with nested lists or tuples.
For small regions, nested collections are helpful. For large ones, a more memory-efficient representation is needed. We explore 2D lists.
First example. Here we create a List with two nested Lists within it. So this example shows a grid of 2 by 2 Ints. With println we can display all elements.

Length: This property returns the count of rows (not of all elements) when using nested lists. Each nested List counts as just 1.

List

Index: We can index within a nested list with the same syntax. And with foreach we can nest lambda expressions.

Lambda
Scala program that uses 2D list // Create list of nested lists. val elements = List(List(20, 30), List(40, 50)) // Print elements. println(elements) println(elements.length) // Access top left element. val topLeft = elements(0)(0) println("Top left = " + topLeft) // Loop over all elements with a foreach call. elements.foreach(_.foreach(println(_))) Output List(List(20, 30), List(40, 50)) 2 Top left = 20 20 30 40 50
Nested tuples. Here we form a List of tuples (pairs). We add another pair to the beginning of our list, creating a new, modified list. We then enumerate our list with for.TupleFor

Finally: We print each item in the nested tuples. This is a representation of 4 rows and 2 columns, a 2D structure.

println
Scala program that uses list, tuple elements // Create list of tuples. val elements = List((10, 20), (30, 40), (50, 60)) // Add a tuple to the start of the list. val modified = (0, 10) :: elements // Loop over indices in the list. for (pair <- modified) { // Get both parts of the tuple. val left = pair._1 val right = pair._2 // Print parts of the tuple. println("Left = " + left + "; Right = " + right) } Output Left = 0; Right = 10 Left = 10; Right = 20 Left = 30; Right = 40 Left = 50; Right = 60
Array.ofDim. Here is a 2D array. This is a more efficient, low-level representation of two-dimensional data. We create the array with the Array.ofDim function.Array

Arguments: The two arguments to Array.ofDim are the row count and column count. We must provide a type argument.

Apply: We use the apply() and update() functions to change cells in our 2D array. With apply we access rows—these are 1D arrays.

Update: On the rows, we use update() to change the value of cells. Finally we use nested for-loops to print all Int values.

Scala program that uses Array.ofDim, 2D array // Create a 2D array of 3 rows and 3 columns. val numbers = Array.ofDim[Int](3, 3) // Update first row cells. val result1 = numbers.apply(0) result1.update(0, 900) result1.update(1, 800) result1.update(2, 700) // Update second row. val result2 = numbers.apply(1) result2.update(0, -1) // Update third row. val result3 = numbers.apply(2) result3.update(0, -2) // Loop over rows. for (row <- numbers) { // Loop over cells in rows. // ... Display all numbers on lines. for (cell <- row) { print(cell) print(" ") } println() } Output 900 800 700 -1 0 0 -2 0 0
More dimensions. In advanced physics, more dimensions are needed to understand our universe. In Scala, we build 1 to 5 dimensional arrays with the Array.ofDim function.

Tip: Array.ofDim overloads are available. But higher-dimensional arrays tend to be confusing and may harm performance.

Often, small 2D collections can be represented in simple, straightforward ways in Scala. We can nest Lists within Lists, or even nest related types like classes or tuples.Class
© 2007-2019 Sam Allen. Every person is special and unique. Send bug reports to info@dotnetperls.com.
Home
Dot Net Perls