In this tutorial you will learn about ArrayList via simple step by step examples.

Example 1: ArrayList

Let us look at a simple ArrayList example written in Kotlin Programming Language. Follow the following steps:

Step 1: Create Project

  1. Open your favorite Kotlin IDE.
  2. In the menu go to File --> Create New Project.

Step 2: Add Dependencies

No dependencies are needed for this project.

Step 3: Write Code

Our code will comprise the following Kotlin files:

Kotlin Android SharedPreferences

Kotlin Android SharedPreferences
Kotlin Android SharedPreferences
  • ArrayList.kt
  1. In your editor or IDE, create a file known as ArrayList.kt.
  2. Then add the following code:

(a). ArrayList.kt

package Collections.ArrayList

fun main(args : Array<String>) {
    val list = arrayListOf(1, 2, 3)
    list.forEach { println(it) }

    val list2 = arrayListOf(Pair("Bill", "Clinton"),
            Pair("George W.", "Bush"),
            Pair ("Barack", "Obama"))

    list2.add(Pair("Hillary", "Clinton"))

    list2.forEach { println("${it.first} - ${it.second}") }


Step 4: Run

Copy the code, build and run.


Here are the reference links:

Number Link
1. Download Example
2. Follow code author
3. Code: Apache 2.0 License