当前位置:首页 > Java

java如何从键盘输入数组

2026-02-05 04:20:15Java

java如何从键盘输入数组

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[] array = new int[size];

        System.out.println("Enter the elements of the array:");
        for (int i = 0; i < size; i++) {
            array[i] = scanner.nextInt();
        }

        System.out.println("Array elements are:");
        for (int num : array) {
            System.out.print(num + " ");
        }
        scanner.close();
    }
}

使用BufferedReader类读取数组

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Enter the size of the array: ");
        int size = Integer.parseInt(reader.readLine());
        int[] array = new int[size];

        System.out.println("Enter the elements of the array separated by space:");
        String[] input = reader.readLine().split(" ");
        for (int i = 0; i < size; i++) {
            array[i] = Integer.parseInt(input[i]);
        }

        System.out.println("Array elements are:");
        for (int num : array) {
            System.out.print(num + " ");
        }
        reader.close();
    }
}

使用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();
        String[] array = new String[size];
        scanner.nextLine(); // Consume the newline

        System.out.println("Enter the elements of the array:");
        for (int i = 0; i < size; i++) {
            array[i] = scanner.nextLine();
        }

        System.out.println("Array elements are:");
        for (String str : array) {
            System.out.println(str);
        }
        scanner.close();
    }
}

使用命令行参数输入数组

public class Main {
    public static void main(String[] args) {
        if (args.length == 0) {
            System.out.println("No arguments provided.");
            return;
        }

        System.out.println("Array elements are:");
        for (String arg : args) {
            System.out.print(arg + " ");
        }
    }
}

注意事项

  • 使用Scanner时,注意处理输入缓冲区中的换行符。
  • 使用BufferedReader时,需要处理IOException
  • 输入数据时,确保数据类型与数组类型匹配,避免NumberFormatException

以上方法可以根据具体需求选择适合的方式从键盘输入数组。

分享给朋友:

相关文章

java如何创建数组

java如何创建数组

创建数组的基本方法 在Java中,数组是固定大小的同类型元素集合。创建数组需要声明数组类型并初始化。 声明数组 语法格式为 数据类型[] 数组名 或 数据类型 数组名[]: int[] arr1;…

vue实现数组倒叙

vue实现数组倒叙

实现数组倒序的方法 在Vue中实现数组倒序可以通过多种方式完成,以下是几种常见的方法: 使用JavaScript的reverse()方法 // 在data中定义数组 data() { retu…

vue实现数组置顶操作

vue实现数组置顶操作

实现数组置顶的方法 在Vue中实现数组置顶操作,可以通过多种方式完成。以下是几种常见的实现方法: 使用数组的splice和unshift方法 // 假设有一个数组items const index…

vue实现添加数组

vue实现添加数组

Vue 实现添加数组的方法 在 Vue 中,可以通过多种方式实现向数组添加元素。以下是几种常见的方法: 使用 push 方法 通过 Vue 的响应式系统,直接调用数组的 push 方法添加元素:…

vue 数组响应式实现

vue 数组响应式实现

Vue 数组响应式实现原理 Vue 通过重写数组的原型方法来实现对数组操作的响应式监听。具体实现基于以下机制: 拦截数组变异方法 Vue 创建了一个继承自原生 Array 原型的对象,并重写了以下会…

php数组的内部实现

php数组的内部实现

PHP数组的内部实现 PHP的数组实际上是一个有序的哈希表(Hash Table),它结合了链表和哈希表的特性,既能通过数字索引快速访问,也能通过字符串键高效查找。 哈希表结构 PHP数组的核心是z…