kotlin键值对数组_Kotlin程序检查数组是否包含给定值
生活随笔
收集整理的這篇文章主要介紹了
kotlin键值对数组_Kotlin程序检查数组是否包含给定值
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
kotlin鍵值對數組
Given an array and an element, we have to check whether array contains the given element or not.
給定一個數組和一個元素,我們必須檢查數組是否包含給定的元素。
Example:
例:
Input:arr = [34, 56, 7, 8, 21, 0, -6]element to check: 21Output:21 found at 4 index程序檢查數組是否在Kotlin中包含給定值 (Program to check if an array contains a given value in Kotlin)
package com.includehelpimport java.util.*//Main Function entry Point of Program fun main(args: Array<String>) {//Input Streamval scanner = Scanner(System.`in`)//Input Array Sizeprint("Enter number of elements in the array: ")val size = scanner.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] = scanner.nextInt()}//Print All Array Elementsprintln("Array : ${intArray.contentToString()} ")//input integer no to be find in arrayprint("Enter Integer Number to be Searched in Array : ")val num = scanner.nextInt()var isFound= falsevar itemAt = 0//Search Given number into Arrayfor(item in intArray){if(item==num){isFound=trueitemAt =intArray.indexOf(item)break}}//Alternatively we can also use contains(num) method of Arrays Class //in kotlin to find specific elements Array or not//var isFound = intArray.contains(num)if(isFound){println("$num found in Arrays at Index $itemAt")}else{System.err.println("$num Not found in Arrays !!")} }Output
輸出量
Run 1: ----- Enter number of elements in the array: 7 Enter Arrays Elements: intArray[0] : 34 intArray[1] : 56 intArray[2] : 7 intArray[3] : 8 intArray[4] : 21 intArray[5] : 0 intArray[6] : -6 Array : [34, 56, 7, 8, 21, 0, -6] Enter Integer Number to be Searched in Array : 21 21 found in Arrays at Index 4 -------- Run 2: ---- Enter number of elements in the array: 7 Enter Arrays Elements: intArray[0] : 3 intArray[1] : 4 intArray[2] : 5 intArray[3] : 7 intArray[4] : 90 intArray[5] : -89 intArray[6] : 5 Array : [3, 4, 5, 7, 90, -89, 5] Enter Integer Number to be Searched in Array : 345 345 Not found in Arrays !!翻譯自: https://www.includehelp.com/kotlin/check-if-an-array-contains-a-given-value.aspx
kotlin鍵值對數組
總結
以上是生活随笔為你收集整理的kotlin键值对数组_Kotlin程序检查数组是否包含给定值的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java LocalDate类| 带示例
- 下一篇: python独立log示例_带有Pyth