Ad space (banner)
๐ŸŽฏKotlin Lessons
Lesson 24 / 68

Two-Dimensional Lists (Tabular Data)

In this lesson you will learn how to use two-dimensional lists in Kotlin so you can work with data laid out as a table. This is for anyone searching for "Kotlin 2D list how to use".

Putting lists inside a list gives you a grid with rows and columns. This is called a two-dimensional list, and you access it with two indexes, as in grid[row][column].

The sample code declares two rows of three columns with listOf(listOf(1, 2, 3), listOf(4, 5, 6)) and reads a value with two sets of square brackets, as in grid[1][2]. Notice how for (row in grid) takes each row in turn.

A common stumbling block for beginners is how the data is actually laid out in memory. It looks like a table, but knowing that internally it is a collection of independent lists, one per row, deepens your understanding.

In real-world development this idea applies whenever you handle grid-shaped data, from a game board with coordinates to spreadsheet-like data.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run Kotlin directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)