kotlin array

Array in kotlin is a generic class not an object like in java language. Arrays are created with various arrayOf functions. There are a couple of ways to create an array in kotlin:

  • val a= arrayOf (E); val a=arrayOfNulls(E); //E is the list of elements of the array you want to create separated by a comma.
  • val a: Array<T>=arrayOf(E); //Array constructor

//array of int


val a =arrayOfNulls<Int>(2);// empty array of two elements.
a[0]=0; //defining the first element
a[1]=5; //the second element of the array

val a = arrayOf(0,5,7)

val a: Array<Int> = arrayOf(0,5,7)

//array of String

val a = arrayOf(“hi”, “hello”, “hola”)

val b: Array<String> = arrayOf(“hi”, “hello”, “hola”)

We have these functions with them we can create arrays in kotlin:

fun doubleArrayOf(vararg elements: Double): DoubleArray

fun floatArrayOf(vararg elements: Float): FloatArray

fun longArrayOf(vararg elements: Long): LongArray

fun intArrayOf(vararg elements: Int): IntArray

fun charArrayOf(vararg elements: Char): CharArray

fun shortArrayOf(vararg elements: Short): ShortArray

fun byteArrayOf(vararg elements: Byte): ByteArray

fun booleanArrayOf(vararg elements: Boolean): BooleanArray

fun <reified @PureReifiable T> arrayOfNulls(size: Int): Array<T?>

اترك تعليقاً

لن يتم نشر عنوان بريدك الإلكتروني. الحقول الإلزامية مشار إليها بـ *

جميع الحقوق محفوظة لموقع كيفاش 2024