
- Android Basics
- Android - Home
- Android - Overview
- Android - Environment Setup
- Android - Architecture
- Android - Application Components
- Android - Hello World Example
- Android - Resources
- Android - Activities
- Android - Services
- Android - Broadcast Receivers
- Android - Content Providers
- Android - Fragments
- Android - Intents/Filters
- Android - User Interface
- Android - UI Layouts
- Android - UI Controls
- Android - Event Handling
- Android - Styles and Themes
- Android - Custom Components
- Android Advanced Concepts
- Android - Drag and Drop
- Android - Notifications
- Location Based Services
- Android - Sending Email
- Android - Sending SMS
- Android - Phone Calls
- Publishing Android Application
- Android Useful Examples
- Android - Alert Dialoges
- Android - Animations
- Android - Audio Capture
- Android - AudioManager
- Android - Auto Complete
- Android - Best Practices
- Android - Bluetooth
- Android - Camera
- Android - Clipboard
- Android - Custom Fonts
- Android - Data Backup
- Android - Developer Tools
- Android - Emulator
- Android - Facebook Integration
- Android - Gestures
- Android - Google Maps
- Android - Image Effects
- Android - ImageSwitcher
- Android - Internal Storage
- Android - JetPlayer
- Android - JSON Parser
- Android - Linkedin Integration
- Android - Loading Spinner
- Android - Localization
- Android - Login Screen
- Android - MediaPlayer
- Android - Multitouch
- Android - Navigation
- Android - Network Connection
- Android - NFC Guide
- Android - PHP/MySQL
- Android - Progress Circle
- Android - ProgressBar
- Android - Push Notification
- Android - RenderScript
- Android - RSS Reader
- Android - Screen Cast
- Android - SDK Manager
- Android - Sensors
- Android - Session Management
- Android - Shared Preferences
- Android - SIP Protocol
- Android - Spelling Checker
- Android - SQLite Database
- Android - Support Library
- Android - Testing
- Android - Text to Speech
- Android - TextureView
- Android - Twitter Integration
- Android - UI Design
- Android - UI Patterns
- Android - UI Testing
- Android - WebView Layout
- Android - Wi-Fi
- Android - Widgets
- Android - XML Parsers
- Android Useful Resources
- Android - Questions and Answers
- Android - Useful Resources
- Android - Discussion
Difference between List and Array types in Kotlin
List and array are two popular collections supported by Kotlin. By definition, both these collections allocate sequential memory location. In this article, we will take an example to demonstrate the difference between these two types of collections.
Attribute | Array | List |
---|---|---|
Implementation | Array is implemented using Array<T> class | List<T> or MutableList<T> interfaces are used to implement a List in Kotlin |
Mutable | Array<T> is mutable, i.e., the values can be changed. | List<T> is immutable in nature. In order to create a mutable list, MutableList<T> interface needs to be used. |
Size | Array is of fixed size. It cannot increase and decrease in size. | MutableList<T> do have 'add' and 'remove' functions in order to increase or decrease the size of the MutableList. |
Performance | Use it for better performance, as array is optimized for different primitive data types such as IntArray[], DoubleArray[]. | Use it for better accessibility in the code. As the size is dynamic in nature, hence good memory management. |
Example
In the following example, we will see how we can declare an array and a List in Kotlin and how we can manipulate the values of the same.
fun main(args: Array<String>) { val a = arrayOf(1, 2, 3) // Printing all the values of array a println("The Array contains:") a.forEach{ println(it) } val names = listOf("stud1", "stud2", "stud3") // Printing all the values of list names println("
The List contains: ") names.forEach { println(it) } var days: MutableList<String> = mutableListOf( "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" ) // Printing all the values of MutableList list println("
Given Mutable List contains:") days.forEach{ print(it) } println("
Mutable List after modification:") days.forEach{ print(it + ", ") } }
Output
It will generate the following output −
The Array contains: 1 2 3 The List contains: stud1 stud2 stud3 Given Mutable List contains: MondayTuesdayWednesdayThursdayFridaySaturdaySunday Mutable List after modification: Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday,
- Related Articles
- Difference Between Array and Linked List
- Difference between "fold" and "reduce" in Kotlin
- Difference between "*" and "Any" in Kotlin generics
- Difference between isNullOrEmpty and isNullOrBlank in Kotlin
- Difference Between Array-Based Queue and List-Based Queue
- Equality checks in Kotlin (Difference between "==" and "===" Operators)
- What is the difference between a list and an array in C#?
- Difference between Java and Kotlin in Android with Examples
- Difference between a "class" and an "object" in Kotlin
- What's the difference between "!!" and "?" in Kotlin?
- What is the difference between a python list and an array?
- How to initialize an empty array list in Kotlin?
- What is the difference between "const" and "val" in Kotlin?
- What is the difference between "var" and "val" in Kotlin?
- Difference between fundamental data types and derived data types in C++

Advertisements