kotlin_Kotlin阵列

kotlin

In this tutorial, we’ll look into Kotlin array. If you’re a new to Kotlin, it’s recommended to go through the Introduction to Kotlin tutorial to ensure that you’re upto speed with this tutorial.

在本教程中,我们将研究Kotlin数组。 如果您不是Kotlin的新手,建议您阅读Kotlin简介教程,以确保您熟悉本教程。

Kotlin阵列 (Kotlin Array)

Arrays in Kotlin are not native data structure. Instead they’re a class of the type Array. We can call Kotlin Array as collections class.

Kotlin中的数组不是本机数据结构 。 相反,它们是Array类型的类。 我们可以将Kotlin Array称为collections类。

Kotlin数组声明– arrayOf函数 (Kotlin array declaration – arrayOf function)

Following are the different ways we can initialize an Array. To initialize and set the elements of an Array, we use the arrayOf function.

以下是我们初始化数组的不同方法。 要初始化和设置Array的元素,我们使用arrayOf函数。

val countries = arrayOf("India","USA","China","Australia","Sri Lanka")

The Kotlin compiler implicitly defers the type of the Array.
To specify the type we can set the function as :

Kotlin编译器隐式推迟了Array的类型。
要指定类型,我们可以将函数设置为:

val countries = arrayOf<String>("India","USA","China","Australia","Sri Lanka") //an array of Strings

A generic array of type Any can be defined as :

类型为Any通用数组可以定义为:

val myArray = arrayOf("Hi",1,1L,1.2,false)
val array1 = arrayOf(1,2,3,4)
val array2 = arrayOf(1L,2L,3L,4L)
val array3 = arrayOf<Long>(1,2,3,4) //this is the same as array2. An array of longs

访问和修改数组中的元素 (Accessing and Modifying Elements in Array)

Typically, we use the index of the array to access and modify elements of an array. In Kotlin Arrays, we can additionally use get and set methods as shown below.

通常,我们使用数组的索引来访问和修改数组的元素。 在Kotlin数组中,我们可以另外使用getset方法,如下所示。

val array1 = arrayOf(1,2,3,4)
val array3 = arrayOf<Long>(1,2,3,4)

array3.get(0)
array3[0]

array1[1] = 6
array1.set(1,6)

println("Size is ${array1.size}") //Size is 4

Note: To get the length of the array, we invoke the function size

注意:要获取数组的长度,我们调用函数size

Kotlin阵列的实用程序功能 (Utility Functions for Kotlin Array)

Kotlin provides us utility functions to initialise arrays of primitives using functions such as :
charArrayOf(), booleanArrayOf(), longArrayOf(), shortArrayOf(), byteArrayOf().

Kotlin为我们提供了一些实用函数,可使用以下函数来初始化基元数组:
charArrayOf()booleanArrayOf()longArrayOf()shortArrayOf()byteArrayOf()

Using these functions would compile the Array classes into int[], char[], byte[] etc.

使用这些函数会将Array类编译为int[]char[]byte[]等。

Additionally, these primitive type arrays are handy when your codebase contains Kotlin as well as Java code. Kotlin type arrays can be passed to the Java code as primitive type arrays. Furthermore, these primitive type arrays boost the performance of the project.

此外,当您的代码库包含Kotlin和Java代码时,这些原始类型数组也很方便。 Kotlin类型数组可以作为原始类型数组传递给Java代码。 此外,这些原始类型数组可提高项目的性能。

val array1 = intArrayOf(1,2,3,4)

Kotlin Array()构造函数 (Kotlin Array() Constructor)

The constructor expects two parameters. The size and init. We specify a lambda expression in the init parameter as shown below.

构造函数需要两个参数。 sizeinit 。 我们在init参数中指定一个lambda表达式 ,如下所示。

val array = Array(6) {i-> i*2}
//or
val array = Array(6,{i-> i*2})

for(element in array)
println(element)

In the above code, the second argument takes in a lambda function, which takes the index of the array element and then returns the value to be inserted at that index in the array.

在上面的代码中,第二个参数接受一个lambda函数,该函数获取数组元素的索引,然后返回要在数组中该索引处插入的值。

val array = Array(6) {i-> i*0.1} // Array of Doubles

for(element in array)
println(element)
val array = Array(6) {i-> "Hi "+i}

for(element in array)
println(element)

//prints the following in the console
Hi 0
Hi 1
Hi 2
Hi 3
Hi 4
Hi 5

The compiler would throw an error if an Array Constructor’s size is set but it’s not initialized as shown below.

如果设置了数组构造函数的大小,但未初始化,则编译器将引发错误,如下所示。

var intArray = Array(6) //would not compile
var intArray2 : Array<IntArray> //this is fine. Since we've just defined the type of var.

When using a primitive type of Array as shown below, instantiation isn’t necessary, as shown below.

如下所示,使用Array的原始类型时,不需要实例化,如下所示。

val intArray = IntArray(6)

    for(element in intArray)
    {
        println(element) //all elements are zero here.
    }

Allocating the size to a primitive type array will assign all the elements a default value if not instantiated otherwise.

如果未实例化,则将大小分配给基本类型数组将为所有元素分配默认值。

将泛型数组转换为原始数组 (Converting Generic Array to Primitive Array)

A generic Array class can be converted into a primitive array type by invoking the method toIntArray(), toCharArray() etc on the generic array instance. Following snippet demonstrates the same.

通过在通用数组实例上调用方法toIntArray()toCharArray()等,可以将通用Array类转换为原始数组类型。 以下片段演示了相同的内容。

val array1 = arrayOf(1,2,3,4)
var intArray1 : IntArray = array1.toIntArray()

Kotlin阵列范例 (Kotlin Array Examples)

  1. 反转数组 (Reversing an array)
    var array1 = arrayOf(1,2,3,4)
    array1 = array1.reversedArray()
        for(element in array1)
        {
            println(element)
        }
    //Prints 4,3,2,1

  2. 反转阵列 (Reversing an Array in place)
    var array1 = arrayOf(1,2,3,4)
    array1.reverse()
        for(element in array1)
        {
            println(element)
        }
    //prints 4,3,2,1

  3. 数组元素的总和 (Sum of elements of an Array)
    var array1 = arrayOf(1,2,3,4)
    println(array1.sum()) //prints 10

  4. 在数组中附加元素 (Appending an Element in an Array)
    var array1 = arrayOf(1,2,3,4)
    array1 = array1.plus(5)
    //or
    array1 = array1.plusElement(5)
        for(element in array1)
        {
            println(element)
        }
    //prints 1,2,3,4,5

  5. 用给定元素填充索引范围 (Fill the range of indexes with the given element)
    var array1 = arrayOf(1,2,3,4)
    array1.fill(0,0,array1.size)
        for(element in array1)
        {
            println(element)
        }
    //prints 0,0,0,0,0

    Note: The fill method isn’t constrained to the current size of the array. We can exceed the array’s index limit to increase the length of array too as shown below.

    注意:fill方法不受限于当前数组的大小。 我们也可以超出数组的索引限制来增加数组的长度,如下所示。

  6. 将数组追加到当前数组 (Append an array to the current array)
    var oldArray = Array(6, {i->i*10})
    array1.fill(0,0,array1.size)
    array1 = array1.plus(oldArray)
    
        for(element in array1)
        {
            println(element)
        }

That’s all for kotlin array example, we will use it a lot in further kotlin tutorials and projects.

这就是kotlin数组示例的全部内容,我们将在以后的kotlin教程和项目中大量使用它。

Reference: Array API Doc

参考: Array API文档

翻译自: https://www.journaldev.com/17339/kotlin-array

kotlin

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值