需要将数组传递给另一个类

时间:2014-09-07 21:34:30

标签: java arrays

我正在研究流体力学问题,我决定使用Java而不是excel解决问题,因为我只是想要体验。在过去的4个小时里,我一直坚持这一部分。我有一个我在一个类中创建的数组,我需要将所有数据传递给另一个类,以便我可以绘制它。在这一点上,我真的只是在寻找答案。我的思绪现在正在拍摄,我只想完成这件事。

我发现其他人有类似的问题,但我无法理解他们给出的解决方案,现在我的大脑已经被炸了,这就是为什么我更愿意如果有人能够告诉我到底输入的是什么,但任何帮助表示赞赏。

public class Eight_c {

public static void main(String [] args) {
    Eight_c ball = new Eight_c();

    ...

    int count = 0;
    double[] y = new double[101];
    double[] x = new double[101];


     // Calculates the Distance the ball has traveled using increments of .1s
     for(double t = 0; t<=time; t=t+.1) {

         y[count] = t;
         x[count] = d;

        V1 = ball.Velocity(a, V2, dt);
        Fd = ball.Drag_Force(V2);
        a = ball.Acceleration(Fd, m);
        d = ball.Distance(V2, a, dt) + d;
    ...

以上是我创建两个数组x和y的地方。

以下是他们需要去的地方。

public class Graph {

public static void main(String [] args) {

    double [] x;
    double [] y;

    // create your PlotPanel (you can use it as a JPanel)
      Plot2DPanel plot = new Plot2DPanel();

      // add a line plot to the PlotPanel
      plot.addLinePlot("Distance vs Time", x, y);

      // put the PlotPanel in a JFrame, as a JPanel
      JFrame frame = new JFrame("a plot panel");
      frame.setContentPane(plot);
      frame.setVisible(true);
    }
}

1 个答案:

答案 0 :(得分:1)

main()是程序的切入点。你不能有2个入口点。所以,你需要第一个类的main方法来调用第二个类的方法,并将x和y作为参数给出:

public class Graph {

    public void render(double[] x, double[] y) {

        // create your PlotPanel (you can use it as a JPanel)
        Plot2DPanel plot = new Plot2DPanel();

        // add a line plot to the PlotPanel
        plot.addLinePlot("Distance vs Time", x, y);

        // put the PlotPanel in a JFrame, as a JPanel
        JFrame frame = new JFrame("a plot panel");
        frame.setContentPane(plot);
        frame.setVisible(true);
    }
}

并在Eight_c的主要方法中:

// create a Graph object:
Graph graph = new Graph();

// ask it to render x and y:
graph.render(x, y);

如果你不知道什么方法和对象,以及如何将参数传递给方法,我发现开始使用Swing很奇怪。当你还没学会如何走路时,这有点像试飞空中客车。

阅读有关Java和编程的介绍性书籍。使用简单的基于控制台的程序进行练习和练习。然后才开始使用Swing。