- import java.util.Scanner;
- public class ArrayExample {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- // Ask the user how many elements to input
- System.out.print("Enter the number of elements: ");
- int t = scanner.nextInt();
- // Declare an array to hold the elements
- int[] array = new int[t];
- // Read t elements into the array
- System.out.println("Enter " + t + " elements:");
- for (int i = 0; i < t; i++) {
- array[i] = scanner.nextInt();
- }
- // Display the elements of the array
- System.out.println("The elements in the array are:");
- for (int i = 0; i < t; i++) {
- System.out.print(array[i] + " ");
- }
- }
- }