📜  打印数组中第 k 个元素的Java程序

📅  最后修改于: 2022-05-13 01:55:04.817000             🧑  作者: Mango

打印数组中第 k 个元素的Java程序

我们需要打印给定数组中第 k 个位置的元素。因此,我们通过从用户那里获取有关数组大小的输入以及该数组的所有元素来启动程序。现在通过输入要打印数组元素的位置 k,程序将打印数组中k-1位置的元素。

注意: Java数组中元素的索引总是从 0 开始,一直到数组大小以下的数字 1。因此,对于具有 10 个元素的数组,索引将从 0 到 9。

例子

Input: 5 (size of an array)
       1, 2, 3, 4, 5 (array elements)
       3 (position k)
Output: 3

Input: 5
       5, 3, 7, 1, 9
       3
Output: 7
Java
// Java Program to Print the kth Element in the Array
  
import java.io.*;
import java.util.Scanner;
  
class GFG {
    public static void main(String[] args)
    {
        {
            int n;
  
            // scanner object to acces user input
            Scanner s = new Scanner(System.in);
            System.out.print(
                "Enter number of elements you want in array:");
  
            // storing input in variable
            n = s.nextInt();
  
            // create int array
            int a[] = new int[n];
            System.out.println("Enter all the elements:");
  
            // iterating for loop to enter the values in the
            // array
            for (int i = 0; i < n; i++) {
                a[i] = s.nextInt();
            }
  
            System.out.print(
                "Enter the k th position at which you want to check number:");
            int k = s.nextInt();
            System.out.println("Number is :" + a[k - 1]);
        }
    }
}


输出

Enter number of elements you want in array: 5
Enter all the elements: 
5
3
7
1
9
Enter the k th position at which you want to check number: 3
Number is : 7