如何将命令行参数放入数组方法?

时间:2017-12-01 19:33:20

标签: java arrays

我需要创建一个具有setter的类来为数组赋值,然后在main方法中获取命令行参数并使用该方法将其放入数组中。我不知道如何做到这一点。任何帮助将不胜感激。

import java.util.*;
public class Number{

 private double [] number = new double[3];  
 private double value ;
 private int i;

 public double[] getNumber() {  

    return sweet;
}

 public void printNumber() {

    System.out.println("Array " + Arrays.toString(number));          
}

public double getValue(int i) {   

    return this.i;
}
public void setMethod(int i, double value) {

    this.value = value;
    this.i = i;
}
 public class Score {

  public static void main (String [] args) {

   Number score = new Number();
// code to get values from keyboard into the array

编辑:感谢您的帮助,我成功创建了新阵列。现在我需要能够更改数组值。在我的setMethod中,我猜我需要将其更改为类似的东西..,

   public void setMethod(int i, double value {      //

for ( i = 0; i < this.array.length; i ++){
                this.array[this.i] =this. value;
}

                this.mark = mark;
                this.pos = pos;


}

2 个答案:

答案 0 :(得分:1)

如果查看main()方法的参数列表,您将看到String[] args - 命令行参数作为参数传递给main()方法。您只需使用for循环读取它们:

String[] yourNewArray = new String[args.length]:
for(int i = 0; i< args.length; i++) {
    yourNewArray[i] = args[i];
}

现在在yourNewArray中存储了命令行参数。

值得一提的是,yourNewArray并不需要是一个包含Strings的数组。作为命令行参数传递的参数可以被解析并用作例如整数,双精度值和其他类型的值。

现在,当您编辑问题并想出新的东西时,我将向您展示一个示例,如何实现将新数组分配给现有数组的方法以及另一种更改此数组中单个值的方法: / p>

import java.util.*;

// This is your class - there is String[] arr - you want to be able to change whole array or its single value:
class MyClass {

    String[] arr;

    // To change whole array:
    public void setMethod(String[] array) {
        this.arr = array;
    }

    // To change only one value in array:
    public void changeSingleValue(int index, String value) {
        arr[index] = value;
    }

}

public class Test {

    public static void main(String[] args) {

        String[] arrayFromArgs = new String[args.length];
        for(int i = 0; i < args.length; i++) {
            arrayFromArgs[i] = args[i];
        }
        MyClass obj = new MyClass();

        // In this method you assign array storing command line arguments to the array in MyClass:
        obj.setMethod(arrayFromArgs);
        System.out.println("obj.arr: " + Arrays.toString(obj.arr));

        // Here is an example of assigning another array to obj.arr:
        String[] anotherArray = { "A", "B", "C", "D"};
        obj.setMethod(anotherArray);
        System.out.println("obj.arr: " + Arrays.toString(obj.arr));

        // Here is another way to assign new values to obj.arr:
        obj.setMethod(new String[]{"x", "y", "z"});
        System.out.println("obj.arr: " + Arrays.toString(obj.arr));

        // Simple example how to change single value in obj.arr by passing the index where and value that needs to be changed:
        obj.changeSingleValue(1, "Changed");
        System.out.println("obj.arr: " + Arrays.toString(obj.arr));

    }

}

以上程序的输出:

obj.arr: [] // in this array you will see values passed as the command line arguments
obj.arr: [A, B, C, D]
obj.arr: [x, y, z]
obj.arr: [x, Changed, z]

答案 1 :(得分:-2)

尝试使用以下代码复制数组:

public static void main (String [] args) {
   // code to get values from keyboard into the array
    String[] myArgs = new String[args.length];
    for (int i = 0; i < args.length; i++) {
        myArgs[i] = args[i]; 
    }
// ...
}