Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 1221 Vote(s) - 3.55 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to initialize an array in Kotlin with values?

#11
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]

Reply

#12
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)
Reply

#13
<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>`
Reply

#14
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 }
Reply

#15
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)
Reply

#16


In this way, you can initialize the int array in koltin.

val values: IntArray = intArrayOf(1, 2, 3, 4, 5,6,7)






Reply

#17
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.
Reply

#18
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)
Reply

#19
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}
Reply

#20
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]
```
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through