Javaで入力配列の値を表す方法は何ですか?

Javaで、Scannerクラスを使用してコンソールから入力された配列値を取得することができます。以下は例のコードです:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        
        System.out.print("Enter the size of the array: ");
        int size = scanner.nextInt();
        
        int[] arr = new int[size];
        
        System.out.println("Enter the elements of the array:");
        for (int i = 0; i < size; i++) {
            arr[i] = scanner.nextInt();
        }
        
        System.out.println("The entered array is:");
        for (int i = 0; i < size; i++) {
            System.out.print(arr[i] + " ");
        }
        
        scanner.close();
    }
}

この例では、ユーザーには配列のサイズと要素の入力が求められ、入力された配列の値が最終的に印刷されます。

bannerAds