Posts: 0
Threads: 0
Joined: Apr 2020
Reputation:
0
Level: inf []
Total Points: inf
Rank nan / 1
100% to upload Level
Activity inf / 1
99% to upload your Rank
Experience nan
100% to upload Experience
Points: 50
|
You can simply use the existing standard library [methods][1] as shown here:
val numbers = intArrayOf(10, 20, 30, 40, 50)
It might make sense to use a special constructor though:
val numbers2 = IntArray(5) { (it + 1) * 10 }
You pass a size and a lambda that describes how to init the values. Here's the documentation:
/**
* Creates a new array of the specified [size], where each element is calculated by calling the specified
* [init] function. The [init] function returns an array element given its index.
*/
public inline constructor(size: Int, init: (Int) -> Int)
[1]: [To see links please register here]
|
Posts: 0
Threads: 0
Joined: Feb 2020
Reputation:
0
Level: inf []
Total Points: inf
Rank nan / 1
100% to upload Level
Activity inf / 1
99% to upload your Rank
Experience nan
100% to upload Experience
Points: 50
|
In Kotlin we can create array using `arrayOf()`, `intArrayOf()`, `charArrayOf()`, `booleanArrayOf()`, `longArrayOf()` functions.
For example:
var Arr1 = arrayOf(1,10,4,6,15)
var Arr2 = arrayOf<Int>(1,10,4,6,15)
var Arr3 = arrayOf<String>("Surat","Mumbai","Rajkot")
var Arr4 = arrayOf(1,10,4, "Ajay","Prakesh")
var Arr5: IntArray = intArrayOf(5,10,15,20)
|
Posts: 0
Threads: 0
Joined: Dec 2016
Reputation:
0
Level: inf []
Total Points: inf
Rank nan / 1
100% to upload Level
Activity inf / 1
99% to upload your Rank
Experience nan
100% to upload Experience
Points: 50
|
<s>I'm wondering why nobody just gave the most simple of answers:
val array: Array<Int> = [1, 2, 3]
</s>
As per one of the comments to my original answer, I realized this only works when used in annotations arguments (which was really unexpected for me).
Looks like Kotlin doesn't allow to create array literals outside annotations.
For instance, look at this code using @Option from args4j library:
<pre>
@Option(
name = "-h",
aliases = ["--help", "-?"],
usage = "Show this help"
)
var help: Boolean = false
</pre>
The option argument "aliases" is of type `Array<String>`
|
Posts: 0
Threads: 0
Joined: May 2019
Reputation:
0
Level: inf []
Total Points: inf
Rank nan / 1
100% to upload Level
Activity inf / 1
99% to upload your Rank
Experience nan
100% to upload Experience
Points: 50
|
Worth mentioning that when using kotlin builtines (e.g. `intArrayOf()`, `longArrayOf()`, `arrayOf()`, etc) you are not able to initialize the array with default values (or all values to desired value) for a given size, instead you need to do initialize via calling according to class constructor.
// Array of integers of a size of N
val arr = IntArray(N)
// Array of integers of a size of N initialized with a default value of 2
val arr = IntArray(N) { i -> 2 }
|
Posts: 0
Threads: 0
Joined: Dec 2017
Reputation:
0
Level: inf []
Total Points: inf
Rank nan / 1
100% to upload Level
Activity inf / 1
99% to upload your Rank
Experience nan
100% to upload Experience
Points: 50
|
In Java an array can be initialized such as:
int numbers[] = new int[] {10, 20, 30, 40, 50}
But In Kotlin an array initialized many way such as:
Any generic type of array you can use **arrayOf()** function :
val arr = arrayOf(10, 20, 30, 40, 50)
val genericArray = arrayOf(10, "Stack", 30.00, 40, "Fifty")
Using utility functions of Kotlin an array can be initialized
val intArray = intArrayOf(10, 20, 30, 40, 50)
|
Posts: 0
Threads: 0
Joined: Feb 2020
Reputation:
0
Level: inf []
Total Points: inf
Rank nan / 1
100% to upload Level
Activity inf / 1
99% to upload your Rank
Experience nan
100% to upload Experience
Points: 50
|
In this way, you can initialize the int array in koltin.
val values: IntArray = intArrayOf(1, 2, 3, 4, 5,6,7)
|
Posts: 0
Threads: 0
Joined: Oct 2020
Reputation:
0
Level: inf []
Total Points: inf
Rank nan / 1
100% to upload Level
Activity inf / 1
99% to upload your Rank
Experience nan
100% to upload Experience
Points: 50
|
In Kotlin There are Several Ways.
var arr = IntArray(size) // construct with only size
Then simply initial value from users or from another collection or wherever you want.
var arr = IntArray(size){0} // construct with size and fill array with 0
var arr = IntArray(size){it} // construct with size and fill with its index
We also can create array with built in function like-
var arr = intArrayOf(1, 2, 3, 4, 5) // create an array with 5 values
Another way
var arr = Array(size){0} // it will create an integer array
var arr = Array<String>(size){"$it"} // this will create array with "0", "1", "2" and so on.
You also can use `doubleArrayOf()` or `DoubleArray()` or any primitive type instead of Int.
|
Posts: 0
Threads: 0
Joined: Nov 2018
Reputation:
0
Level: inf []
Total Points: inf
Rank nan / 1
100% to upload Level
Activity inf / 1
99% to upload your Rank
Experience nan
100% to upload Experience
Points: 50
|
Simple Way:
**For Integer:**
var number = arrayOf< Int>
(10 , 20 , 30 , 40 ,50)
**Hold All data types**
var number = arrayOf(10 , "string value" , 10.5)
|
Posts: 0
Threads: 0
Joined: Aug 2022
Reputation:
0
Level: inf []
Total Points: inf
Rank nan / 1
100% to upload Level
Activity inf / 1
99% to upload your Rank
Experience nan
100% to upload Experience
Points: 50
|
My answer complements @maroun these are some ways to initialize an array:
Use an array
val numbers = arrayOf(1,2,3,4,5)
Use a strict array
val numbers = intArrayOf(1,2,3,4,5)
Mix types of matrices
val numbers = arrayOf(1,2,3.0,4f)
Nesting arrays
val numbersInitials = intArrayOf(1,2,3,4,5)
val numbers = arrayOf(numbersInitials, arrayOf(6,7,8,9,10))
Ability to start with dynamic code
val numbers = Array(5){ it*2}
|
Posts: 0
Threads: 0
Joined: Feb 2019
Reputation:
0
Level: inf []
Total Points: inf
Rank nan / 1
100% to upload Level
Activity inf / 1
99% to upload your Rank
Experience nan
100% to upload Experience
Points: 50
|
val numbers: IntArray = intArrayOf(10, 20, 30, 40, 50)
See [Kotlin - Basic Types]( [To see links please register here] ) for details.
You can also provide an initializer function as a second parameter:
```kotlin
val numbers = IntArray(5) { 10 * (it + 1) }
// [10, 20, 30, 40, 50]
```
|
|