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 Doublesfor(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

kotlin_Kotlin阵列相关推荐

  1. 曙光服务器bios设置_浪潮服务器PM8060阵列卡,如何做热备盘?操作相对有点复杂...

    春节前安装调试了一台浪潮服务器,型号:5270M5,配置:2颗银牌4210 / 64G 内存 / 服务器固态硬盘:480G / 机械硬盘:8块600G SAS 3.5寸 15000转硬盘 / 1+1冗 ...

  2. 一次HP 阵列卡排障

    公司使用的是HP gen8机器,用的是p420i阵列卡,同时在系统的另一端,nagios监控系统配合nrpe脚本check_hpasm定期检测硬件健康. 最近为了让机器更带劲,加上了SSD硬盘,机械硬 ...

  3. shell脚本编程基础(1)及RAID阵列

    shell脚本: Linux从底层到上层的系统架构:硬件-->内核-->库(lib)-->shell-->用户. shell既是一种命令语言,也是程序设计语言(shell脚本) ...

  4. 计算机视觉方向简介 | 阵列相机立体全景拼接

    点击上方"小白学视觉",选择加"星标"或"置顶" 重磅干货,第一时间送达 有什么用? 全景相机(详见<5分钟了解全景相机>)现在 ...

  5. 影响HP服务器的磁盘性能的重要因素 -- 阵列卡的缓存和电池

    有一台HP DL380服务器,虚拟机上的CentOS 6.3中安装了samba,从Windows拷贝到CentOS时,最初的几分钟10MB/s(网络为百兆有线网络),接下来就逐渐降低,稳定在6MB/s ...

  6. raid卡组不同raid_乐扩IO-PCE9230-4IR阵列卡组阵列教程

    乐扩IO-PCE9230-4IR阵列卡组阵列教程 第一部分: 硬件安装:关闭电脑电源 拔掉电源线,取下机箱盖: 在主板上找一个PCI Express X4,X8,或x16的插槽: 然后将阵列卡小心的插 ...

  7. 利用ISA Server 2006服务器阵列构建高性能、高可靠的企业防火墙

    企业策略: 多"模板"策略              阵列策略继承企业策略 有效策略: 系统策略              企业策略: 阵列前             阵列策略   ...

  8. IBM 3650 M3阵列卡配置

    一般创建阵列卡的步骤如下: 查看阵列卡配置 如果原有配置就删除原有的raid配置,有些服务器默认会给每块盘做raid0 创建raid,选择硬盘,然后创建raid1或者raid5 再次查看配置,保存退出 ...

  9. IBM X3650 M2 BR10i卡的阵列配置方法

    RAID1的步骤: 开机自检过程中出现ctrl+c提示,按ctrl+c进入LSI Logic Config Utility v6.10.02.00(2006.09.27) 1.在SAS1064E上回车 ...

最新文章

  1. [转]centos5.2用memcache 来作PHP 的session.save_handler
  2. 《PowerShell V3——SQL Server 2012数据库自动化运维权威指南》——2.8 创建数据库...
  3. 初识Flink-从WorldCount开始
  4. thymealf如何实现传单个变量给html_纯前端使用JavaScript发送电子邮件,5个步骤图文教程...
  5. 快讯:2018 OOW Oracle技术大会PPT抢鲜下载
  6. JDK源码分析-收藏地址
  7. SQL Server的历史– SQL Server功能的演变
  8. Atitit smb网络邻居原理与实现查询列表
  9. emplace_back() 和 push_back 的区别:emplace_back效率高,避免push_back使用时所需的额外副本或移动操作
  10. Kubernetes Pod
  11. mysql的sql分页查询语句怎么写_sql 分页查询语句(mysql分页语句)
  12. xshell 5中文破解版下载(附注册码)
  13. 新巴巴运动网完整教程
  14. slk文件转换器安卓版_CoolUtils Total Excel Converter下载
  15. Logic Pro X for Mac(专业级音频制作软件)
  16. “确定“和“取消“摆放顺序
  17. win10分辨率不能调整_三国志:游戏在win10系统无法运行咋办?
  18. 【unity shader】毛绒材质效果的实现
  19. 湖南启动CCTV《星光达人秀》 《宾导会客厅》全球直播发布
  20. 大言不惭 swank? talk about sth or speak too confidently

热门文章

  1. 串结构练习--字符串匹配
  2. [原创]项目管理知识体系指南之 13项目干系人管理思维导图
  3. CMMI模型对软件测试技术的扩充
  4. 完全采用CSS的CROSS BROWSER TABBED PAGES
  5. [转载] python-numpy如何初始化数组值全为nan
  6. ng的概念层次(官方文档摘录)
  7. 基于docker的php调用基于docker的mysql数据库的方法
  8. 2101 Problem A Snake Filled
  9. 【转】Pro Android学习笔记(二六):用户界面和控制(14):RelativeLayout
  10. C# partial 说明