使用Java中另一个方法的变量

时间:2016-01-31 19:20:47

标签: java arrays methods

我想计算两个粒子之间不断反应的位置。我的问题是我需要将particle listParticle数组从我的readData()带到我的main(String[] args)方法。

我尝试将粒子数组作为全局变量,但它总是迫使我将其设为静态,然后我的代码不起作用。

我的程序从文件中读取,数据如下:

2 

1 1 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0

2 1 1.5 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0

第一个数字是count,文件中有多少个粒子。每个粒子的位置,力度和力都有IDtypexyz

这是我的代码:

import java.io.File;
import java.io.IOException;
import java.util.*;
import java.io.FileReader;
import java.io.FileNotFoundException;

public class Particle 
{
    int particleID;
    int particleType;
    double particlePosition[] = new double[3];
    double particleVelocity[] = new double[3];
    double particleForce[] = new double[3];
    static int count;
    static int current = 0;
    static Scanner readData;

    public static void main(String[] args) 
    {
        int k = 100; // in [N/m] or [kg*m/s^2]
        int m = 1; // in [kg]
        double x0 = 1; // in [m]
        double t;  // in [s]
        double dt;  //  in [s]
        double oldForce1;
        double oldForce2;
        double curTime = 0;
        double finTime;

        t = 1/((1/(2*(Math.PI))) * Math.sqrt(2*k/m));
        System.out.println(t);

        dt = t/150;

        readfile();

        //System.out.println("First:  [ " + listParticles[0].particlePosition[0] + " , " + 0 + " ]"); <-- does not work
        //System.out.println("Second:  [ " + listParticles[1].particlePosition[0] + " , " + 0 + " ]"); <-- does not work
    }

    public static void readfile()
    {
        try
        {
            readData = new Scanner(new File("src/2particle-initial.data"));
        }
        catch(Exception e)
        {
            System.out.println("Could not find file");
        }
        count = readData.nextInt();

        Particle [] listParticles = new Particle[count];

        while (current < count)
        {
            listParticles[current] = new Particle();
            listParticles[current].particleID = readData.nextInt();
            listParticles[current].particleType = readData.nextInt();

            listParticles[current].particlePosition[0] = readData.nextDouble();
            listParticles[current].particlePosition[1] = readData.nextDouble();
            listParticles[current].particlePosition[2] = readData.nextDouble();

            listParticles[current].particleVelocity[0] = readData.nextDouble();
            listParticles[current].particleVelocity[1] = readData.nextDouble();
            listParticles[current].particleVelocity[2] = readData.nextDouble();

            listParticles[current].particleForce[0] = readData.nextDouble();
            listParticles[current].particleForce[1] = readData.nextDouble();
            listParticles[current].particleForce[2] = readData.nextDouble();

            current++;
        }
        current = 0;

        System.out.println("First:  [ " + listParticles[0].particlePosition[0] + " , " + 0 + " ]");
        System.out.println("Second:  [ " + listParticles[1].particlePosition[0] + " , " + 0 + " ]");

        readData.close();
    }
}

如何将本地readData() listParticle数组发送到main

1 个答案:

答案 0 :(得分:2)

您可以将方法定义为return listParticle数组。像这样:

将其签名更改为:

public static Particle[] readfile()

return

之后添加close语句
return listParticles;

main中,您可以调用此函数并将其返回给变量:

myParticleArray = Particle.readfile();