简体   繁体   English

Java:接受用于对象数组的命令行参数

[英]Java : Accepting command line arguments for array of objects

I have an array like of objects in my main 我的主体中有一个像对象的数组

Number[] NumberArray;

The class Item has this definition: Item类具有以下定义:

public class Number {
    int a1;
    int a2;
}

I need to accept input from command line like say 1 2 3 4 5 6, such that 我需要接受来自命令行的输入,例如1 2 3 4 5 6

Number[1].a1 = 2 

and

Number[1].a2 = 3 

and

Number[4].a1 = 5

and

Number[4].a2 = 6. 

How do I do this? 我该怎么做呢?

Please help. 请帮忙。

In your main method: 在您的主要方法中:

public static void main(String[] args)

args is an array of command line parameters. args是命令行参数的数组。 To convert a String to an int, use Integer.valueOf() . 要将String转换为int,请使用Integer.valueOf()

For example, to get the integer value of the first commandline argument do the following: 例如,要获取第一个命令行参数的整数值,请执行以下操作:

Integer.parseInt(args[0]);

Note that this will crash if there are no arguments. 请注意,如果没有参数,这将崩溃。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM