Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kotlin arrayOfNulls() term entry : Issue #5310 #5593

Merged
merged 10 commits into from
Nov 7, 2024
52 changes: 52 additions & 0 deletions content/kotlin/concepts/arrays/terms/arrayOfNulls/arrayOfNulls.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
---
Title: 'arrayOfNulls()'
Description: 'Creates an array of a specified size with all elements initialized as null.'
Subjects:
- 'Code Foundations'
- 'Mobile Development'
Tags:
- 'Android'
- 'Arrays'
- 'Map'
- 'Kotlin'
CatalogContent:
- 'learn-kotlin'
- 'paths/computer-science'
---

In Kotlin, the **`arrayOfNulls()`** function creates an array of a specified size with all elements initialized to `null`. This function is useful for creating an array with a defined size when initial values are not yet available, allowing for values to be assigned later.

## Syntax

```pseudo
fun <T> arrayOfNulls(size: Int): Array<T?>
```

- `T`: The type of elements in the array.
- `size`: An integer specifying the size of the array to create.

It returns an `Array<T?>` of the specified size, initialized with `null`.

## Example

The following example uses the `arrayOfNulls()` function:

```kotlin
fun main() {
// Create an array of size 5 with all elements initialized as null
val nullArray = arrayOfNulls<Int>(5)

// Assign values to some elements in the array
nullArray[0] = 2
nullArray[1] = 4

// Print the array after assigning values
println(nullArray.contentToString())
}
```

This example results in the following output:

austinwdigital marked this conversation as resolved.
Show resolved Hide resolved
```shell
[2, 4, null, null, null]
```
Loading