插件窝 干货文章 Java数组输入方法详解 - 多种方式实现数组输入

Java数组输入方法详解 - 多种方式实现数组输入

Java数组输入方法详解

在Java编程中,数组是最常用的数据结构之一。本文将详细介绍Java中多种输入数组的方法,帮助开发者掌握数组输入的技巧。

1. 使用Scanner类输入数组

Scanner是Java中最常用的输入类,可以方便地从控制台获取用户输入。

import java.util.Scanner;

public class ArrayInput {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        
        System.out.print("请输入数组长度:");
        int n = scanner.nextInt();
        
        int[] arr = new int[n];
        System.out.println("请输入" + n + "个整数:");
        
        for(int i = 0; i < n; i++) {
            arr[i] = scanner.nextInt();
        }
        
        System.out.println("输入的数组为:");
        for(int num : arr) {
            System.out.print(num + " ");
        }
    }
}

2. 使用BufferedReader输入数组

对于大量数据输入,BufferedReader效率更高。

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

public class ArrayInputBuffered {
    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        
        System.out.print("请输入数组长度:");
        int n = Integer.parseInt(reader.readLine());
        
        int[] arr = new int[n];
        System.out.println("请输入" + n + "个整数(空格分隔):");
        
        String[] inputs = reader.readLine().split(" ");
        for(int i = 0; i < n; i++) {
            arr[i] = Integer.parseInt(inputs[i]);
        }
        
        System.out.println("输入的数组为:");
        for(int num : arr) {
            System.out.print(num + " ");
        }
    }
}

3. 命令行参数输入数组

可以通过main方法的args参数从命令行接收数组。

public class CommandLineArray {
    public static void main(String[] args) {
        if(args.length == 0) {
            System.out.println("请通过命令行参数输入数组元素");
            return;
        }
        
        int[] arr = new int[args.length];
        
        for(int i = 0; i < args.length; i++) {
            arr[i] = Integer.parseInt(args[i]);
        }
        
        System.out.println("输入的数组为:");
        for(int num : arr) {
            System.out.print(num + " ");
        }
    }
}

4. 从文件读取数组

对于大型数组,可以从文件中读取数据。

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class FileArrayInput {
    public static void main(String[] args) throws FileNotFoundException {
        File file = new File("array.txt");
        Scanner scanner = new Scanner(file);
        
        int n = scanner.nextInt();
        int[] arr = new int[n];
        
        for(int i = 0; i < n; i++) {
            arr[i] = scanner.nextInt();
        }
        
        System.out.println("从文件读取的数组为:");
        for(int num : arr) {
            System.out.print(num + " ");
        }
    }
}

5. 二维数组输入方法

二维数组的输入方法与一维数组类似。

import java.util.Scanner;

public class TwoDArrayInput {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        
        System.out.print("请输入行数:");
        int rows = scanner.nextInt();
        
        System.out.print("请输入列数:");
        int cols = scanner.nextInt();
        
        int[][] matrix = new int[rows][cols];
        
        System.out.println("请输入" + rows + "行" + cols + "列的矩阵:");
        
        for(int i = 0; i < rows; i++) {
            for(int j = 0; j < cols; j++) {
                matrix[i][j] = scanner.nextInt();
            }
        }
        
        System.out.println("输入的二维数组为:");
        for(int[] row : matrix) {
            for(int num : row) {
                System.out.print(num + " ");
            }
            System.out.println();
        }
    }
}

总结

本文介绍了Java中输入数组的5种常用方法:

  1. 使用Scanner类从控制台输入
  2. 使用BufferedReader提高输入效率
  3. 通过命令行参数输入
  4. 从文件读取数组数据
  5. 二维数组的输入方法

根据不同的应用场景选择合适的输入方式,可以提高程序的效率和用户体验。对于小型数组,Scanner类简单易用;对于大型数据,建议使用BufferedReader或文件输入。