136

How can I create a Array like we do in java?

int A[] = new int[N];

How can I do this in Kotlin?

1

6 Answers 6

223

According to the reference, arrays are created in the following way:

  • For Java's primitive types there are distinct types IntArray, DoubleArray etc. which store unboxed values.

    They are created with the corresponding constructors and factory functions:

    val arrayOfZeros = IntArray(size) //equivalent in Java: new int[size]
    val numbersFromOne = IntArray(size) { it + 1 }
    val myInts = intArrayOf(1, 1, 2, 3, 5, 8, 13, 21)
    

    The first one is simillar to that in Java, it just creates a primitive array filled with the default value, e.g. zero for Int, false for Boolean.

  • Non primitive-arrays are represented by Array<T> class, where T is the items type.

    T can still be one of types primitive in Java (Int, Boolean,...), but the values inside will be boxed equivalently to Java's Integer, Double and so on.

    Also, T can be both nullable and non-null like String and String?.

    These are created in a similar way:

    val nulls = arrayOfNulls<String>(size) //equivalent in Java: new String[size]
    val strings = Array(size) { "n = $it" } 
    val myStrings = arrayOf("foo", "bar", "baz")
    
    val boxedInts = arrayOfNulls<Int>(size) //equivalent in Java: new Integer[size]
    val boxedZeros = Array(size) { 0 }
    
2
  • 25
    Array<Int>(size) doesn't compile. That's because you can't create a non-nullable array without providing the elements. Feb 8, 2016 at 13:06
  • 3
    @KirillRakhman, thanks for your remark, that was an inadvertent mistake. Fixed.
    – hotkey
    Feb 8, 2016 at 13:57
11

Here is simple example of init of Array of String

        var names = Array<String>(<AnotherArray.size>) { i -> "" }

Kotlin doc

1
  • In practice, it looks like this val array = Array<String?>(10){""} where 10 is me saying an array of 10 empty strings
    – Chucky
    Mar 15, 2022 at 11:30
2

In Kotlin, creating an IntArray of size N is simple. Use IntArray(n) or the appropriate type, as detailed thoroughly in hotkey's answer.

When utilizing your fixed size array, you can use Kotlin Destructuring

// Create fixed sized array
val point = IntArray(2)

// Access values directly
val (x, y) = point

In this case, x will be taken from index 0, y from index 1, etc.

2

If you want to create an array of Primitive Types, you can simply use the constructors:

Example: to create an array of integers with size N.

val arr = IntArray(size = N)

For non-primitive types, you can use arrayOfNulls<Type>().

1
  • Wow that works (arrayOfNulls), but it would have taken me years to figure that out. Thank god kotlin is so obvious and intuitive!
    – SMBiggs
    Nov 15, 2022 at 2:11
1

You can create it using IntArray like this declaration for variable val A = IntArray(N)

-2

val A = FloatArray(N) //N being the size of the ar

1
  • The OP needs an array of integer, not floats.
    – MathMax
    Jan 22, 2021 at 15:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.