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: 21

    Output:
    21 found at 4 index

程序检查数组是否在Kotlin中包含给定值 (Program to check if an array contains a given value in Kotlin)

package com.includehelp

import java.util.*

//Main Function entry Point of Program
fun main(args: Array<String>) {
    //Input Stream
    val scanner = Scanner(System.`in`)

    //Input Array Size
    print("Enter number of elements in the array: ")
    val size = scanner.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] = scanner.nextInt()
    }

    //Print All Array Elements
    println("Array : ${intArray.contentToString()} ")

    //input integer no to be find in array
    print("Enter Integer Number to be Searched in Array : ")
    val num = scanner.nextInt()


    var isFound= false
    var itemAt = 0

    //Search Given number into Array
    for(item in intArray){
        if(item==num){
            isFound=true
            itemAt =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键值对数组

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值