如何调用 java 程序和使用 argv

How to call java Program and to use argv

我想替换 Int[] mydata 并使用数组 argv 并使用 javafx 从另一个 class 调用我的 class 图表 javaX, 从那里我声明我的字符串数组并调用我的 java 程序。 - 现在我需要从另一个程序调用我的 class 字符串,如 mydata. 和 - 使用数组 argv 并构建我的新数组。

这可能吗?如何? 感谢您的帮助。

public class Graph extends Application {

public void start(Stage stage) {

    int[] mydata = {
            12, 9, 0, 1, 38, 19, 21, 72, 33, 83, 14, 10, 65, 46, 10, 17, 27, 38, 65, 98, 8, 58, 38, 79, 37, 69, 26, 15};
    Pane root = new Pane();
    NumberAxis xAxis = new NumberAxis();
    NumberAxis yAxis = new NumberAxis();


    ScatterChart scatterChart=new ScatterChart(xAxis,yAxis);
    scatterChart.setTitle("Chart");
    XYChart.Series data=new XYChart.Series();
    data.setName("Data");
    for (int i=0; i< mydata.length; i++) {
        data.getData().add(new XYChart.Data(i,mydata[i]));
        }

    scatterChart.getData().addAll(data);
    root.getChildren().add(scatterChart);

    Scene scene = new Scene(root, 1000, 600);

    //scene.getStylesheets().add("style.css");
    //scene.getStylesheets().add(getClass().getResource("style.css").toExternalForm());
    scatterChart.setPrefSize(1000, 600);
    stage.setTitle("Graph");
    stage.setScene(scene);

    stage.show();

    File file = new File("c:\Images\Image.png");

    WritableImage snapshot = scene.snapshot(null);
    BufferedImage bimg = SwingFXUtils.fromFXImage(snapshot, null); 
    try {
        ImageIO.write(bimg, "png", file);
    } catch (Exception s) {
        s.printStackTrace();
    }

}

public static void main(String[] argv) {
    launch(argv);
}

}

如果您想在 start 方法中访问 main 方法的 arguments 以便将其转换为整数,您可以调用方法 getParameters().getUnnamed()。更多详情 here.

然后要将 StringList 转换为 IntegerList,您可以这样做:

与Java 8:

List<Integer> mydata = arguments.stream().map(Integer::parseInt).collect(Collectors.toList());

与Java 7:

List<Integer> mydata = new ArrayList<>(arguments.size());
for (String argument : arguments) {
    mydata.add(Integer.parseInt(argument));
}