博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
kotlin键值对数组_Kotlin程序以升序对数组进行排序
阅读量:2534 次
发布时间:2019-05-11

本文共 1807 字,大约阅读时间需要 6 分钟。

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.basicimport java.util.*//Main Function entry Point of Programfun main(args: Array
) { //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: 6Enter Arrays Elements:intArray[0] : 5intArray[1] : 23intArray[2] : 1intArray[3] : 0intArray[4] : 9intArray[5] : 76Ascending Order: 0 1 5 9 23 76----------------Enter number of elements in the array: 8Enter Arrays Elements:intArray[0] : 45intArray[1] : 3intArray[2] : 89intArray[3] : -8intArray[4] : -45intArray[5] : 0intArray[6] : 432intArray[7] : 6Ascending Order: -45 -8 0 3 6 45 89 432

翻译自:

kotlin键值对数组

转载地址:http://gjxzd.baihongyu.com/

你可能感兴趣的文章
站立会议02(一期)
查看>>
oracle数据库导入导出问题
查看>>
Android中的动画
查看>>
LeetCode 119 Pascal's Triangle II
查看>>
【Noip2015pj】求和
查看>>
深入理解JavaScript——闭包
查看>>
C#-WebForm-css box-shadow 给边框添加阴影效果
查看>>
objective-c 编程总结(第七篇)运行时操作 - 动态属性
查看>>
C_数据结构_链表
查看>>
kettle-连接控件
查看>>
Coursera--Neural Networks and Deep Learning Week 2
查看>>
C#中的委托和事件(续)【来自张子扬】
查看>>
机器学习部分国内牛人
查看>>
模拟Sping MVC
查看>>
Luogu 3261 [JLOI2015]城池攻占
查看>>
java修饰符
查看>>
C# Using 用法
查看>>
javascript函数的几种写法集合
查看>>
BZOJ第1页养成计划
查看>>
漫漫修行路
查看>>