kotlin键值对数组

Given an array, we have to sort its elements in ascending order.

给定一个数组,我们必须按升序对其元素进行排序。

Example:

例:

    Input:
arr = [10, 20, 5, 2, 30]
Output:
sorted array (Ascending Order): [2, 5, 10, 20, 30]

在Kotlin中以升序对数组进行排序的程序 (Program to sort an array in ascending order in Kotlin)

package com.includehelp.basic
import java.util.*
//Main Function entry Point of Program
fun main(args: Array<String>) {
//Input Stream
val s = Scanner(System.`in`)
//Input Array Size
print("Enter number of elements in the array: ")
val size = s.nextInt()
//Create Integer array of Given size
val intArray = IntArray(size)
//Input array elements
println("Enter Arrays Elements:")
for (i in intArray.indices) {
print("intArray[$i] : ")
intArray[i] = s.nextInt()
}
//to Perform Ascending Order Sorting on integer Array
var temp:Int
for (i in intArray.indices) {
for (j in i + 1 until intArray.size) {
if (intArray[i] > intArray[j]) {
temp = intArray[i]
intArray[i] = intArray[j]
intArray[j] = temp
}
}
}
//Alternatively we can also use sort() method of Arrays
//Class in kotlin to sort in Ascending Order
//intArray.sort()
print("Ascending Order: ")
for (item in intArray) {
print("$item ")
}
}

Output

输出量

Enter number of elements in the array: 6
Enter Arrays Elements:
intArray[0] : 5
intArray[1] : 23
intArray[2] : 1
intArray[3] : 0
intArray[4] : 9
intArray[5] : 76
Ascending Order: 0 1 5 9 23 76
----------------
Enter number of elements in the array: 8
Enter Arrays Elements:
intArray[0] : 45
intArray[1] : 3
intArray[2] : 89
intArray[3] : -8
intArray[4] : -45
intArray[5] : 0
intArray[6] : 432
intArray[7] : 6
Ascending Order: -45 -8 0 3 6 45 89 432

翻译自: https://www.includehelp.com/kotlin/sort-an-array-in-ascending-order.aspx

kotlin键值对数组

kotlin键值对数组_Kotlin程序以升序对数组进行排序相关推荐

  1. kotlin键值对数组_Kotlin程序检查数组是否包含给定值

    kotlin键值对数组 Given an array and an element, we have to check whether array contains the given element ...

  2. PHP将一个二维数组按照某个键的键值做出重组一个新的二维数组

    如下一个二维数组,将一个二维数组按照apname的 键值来排序组合成一个新的二维数组  apname 键值不变,其中其他几个红色划线部分进行组合. 构建好之后展现效果如下 使用了两种方式进行构建 方法 ...

  3. java char 动态数组_Java程序来填充char数组中的元素

    可以使用java.util.Arrays.fill()方法将元素填充到char数组中.此方法将所需的char值分配给Java中的char数组.所需的两个参数是数组名称和要存储在数组元素中的值. 演示此 ...

  4. java 将set转成数组,Java程序将Set转换为数组

    Set对象提供一种称为的方法toArray().此方法接受一个空数组作为参数,将当前Set转换为数组并将其放置在给定数组中.要将Set对象转换为数组-创建一个Set对象. 向其添加元素. 创建一个具有 ...

  5. c#sort升序还是降序_C#中对数组或集合进行升序或降序排序

    在.net framework中,数组和集合都实现了用于排序的方法sort(),数组通过调用Array.Sort(数组名)排序,集合通过调用 集合对象.Sort()方法排序. 默认的排序是使用升序排列 ...

  6. kotlin字符串数组_Kotlin程序读取,遍历,反向和排序字符串数组

    kotlin字符串数组 Given a string array, we have to read, traverse, reverse and sort its elements. 给定一个字符串数 ...

  7. kotlin 扩展类的功能_Kotlin程序| 扩展功能功能

    kotlin 扩展类的功能 扩展功能 (Extension function) Kotlin provides the ability to add more functionality to the ...

  8. kotlin 两个数字相加_Kotlin程序交换两个数字

    kotlin 两个数字相加 Given two numbers, we have to swap them. 给定两个数字,我们必须交换它们. Example: 例: Input: First num ...

  9. 疯狂Kotlin讲义学习笔记04-05章:流程控制、数组和集合

    1.when分支取代swith分支 不在需要使用case关键字 case后面的冒号改为-> default改为更有意义的else 如果一个case里有多条语句,需要将多条语句用大括号括起来 wh ...

最新文章

  1. Java FutureTask示例程序
  2. Android 截图,截取指定view截图
  3. Everything Toolbar – 用 Everything 替换 Win 10 任务栏系统搜索框
  4. python 自动回收机制
  5. 精益质量管理之做好质量管理工作的三大管理手法
  6. 腾讯云即时通讯IM前端使用步骤简述
  7. 深度 GHOST XP SP3 纯净版 2013 06
  8. html计算斐波那契数列通项公式,斐波那契数列指标公式
  9. IE无法打开网页的问题
  10. 原神pc端服务器切换工具,基于python3
  11. JavaScript实现在线Markdown编辑器、转换HTML工具-toolfk程序员工具网
  12. IDEA 卡住半天,buid(编译)不动——解决办法(适用于maven和gradle)及定位思路...
  13. Chrome浏览器 v71.0.3578.99
  14. 腾讯阿里是否开始走向衰落,用新互联网大脑模型分析
  15. lscat cnindex php,BJDCTF2020--web-复现
  16. Regeorg 从python2改造为python3-从实战中学习socks5协议
  17. 用二十来页PPT做个年终总结及规划
  18. Softmax函数下的交叉熵损失含义与求导
  19. 齿轮数据接口标准及应用前景
  20. 从大专生到蚂蚁金服CTO,他写下“支付宝”第一行代码:逆风的方向,更适合飞翔!...

热门文章

  1. java 内存数据库 sql_ssm+redis +Maven用于内存数据库 (No Sql)入门demo
  2. 深入理解表单脚本系列第一篇——表单对象
  3. 6个经典的JavaScript报错分析
  4. scss的使用方式(环境搭建)
  5. Pyechart:30分钟学会pyecharts数据可视化
  6. springboot 工程启动报错之Consider defining a bean of type ‘XXX’ in your configuration.
  7. python - classs内置方法 solt
  8. Shell 简单的java微服务jar包 -- 部署脚本
  9. python链表的实现,有注释
  10. ScanTailor-ScanTailor 强大的多方位的满足处理扫描图片的需求