kotlin键值对数组_Kotlin程序以升序对数组进行排序
生活随笔
收集整理的這篇文章主要介紹了
kotlin键值对数组_Kotlin程序以升序对数组进行排序
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
kotlin鍵值對(duì)數(shù)組
Given an array, we have to sort its elements in ascending order.
給定一個(gè)數(shù)組,我們必須按升序?qū)ζ湓剡M(jìn)行排序。
Example:
例:
Input:arr = [10, 20, 5, 2, 30]Output:sorted array (Ascending Order): [2, 5, 10, 20, 30]在Kotlin中以升序?qū)?shù)組進(jìn)行排序的程序 (Program to sort an array in ascending order in Kotlin)
package com.includehelp.basicimport java.util.*//Main Function entry Point of Program fun main(args: Array<String>) {//Input Streamval s = Scanner(System.`in`)//Input Array Sizeprint("Enter number of elements in the array: ")val size = s.nextInt()//Create Integer array of Given sizeval intArray = IntArray(size)//Input array elementsprintln("Enter Arrays Elements:")for (i in intArray.indices) {print("intArray[$i] : ")intArray[i] = s.nextInt()}//to Perform Ascending Order Sorting on integer Arrayvar temp:Intfor (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鍵值對(duì)數(shù)組
總結(jié)
以上是生活随笔為你收集整理的kotlin键值对数组_Kotlin程序以升序对数组进行排序的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 颐和园通票和门票有什么不同
- 下一篇: 用C#开发Windows应用程序