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

在Java中使用来自另一个方法的变量,java,arrays,methods,Java,Arrays,Methods,我想计算两个粒子的位置,这两个粒子会不断地相互反应。我的问题是,我需要将我的particle list particle数组从我的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 第

我想计算两个粒子的位置,这两个粒子会不断地相互反应。我的问题是,我需要将我的
particle list particle
数组从我的
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
,即文件中有多少粒子。每个粒子都有一个用于位置、速度和力的
ID
类型
、和
x
y
、和
z

这是我的密码:

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();
    }
}
导入java.io.File;
导入java.io.IOException;
导入java.util.*;
导入java.io.FileReader;
导入java.io.FileNotFoundException;
公共类粒子
{
int particleID;
int粒子型;
双粒子位置[]=新双粒子[3];
double particleVelocity[]=新的double[3];
双粒子力[]=新的双粒子力[3];
静态整数计数;
静态电流=0;
静态扫描仪读取数据;
公共静态void main(字符串[]args)
{
int k=100;//英寸[N/m]或[kg*m/s^2]
int m=1;//英寸[kg]
双x0=1;//英寸[m]
双t;//在[s]中
双dt;//in[s]
双奥尔德福斯1;
双oldForce2;
双缩短时间=0;
双finTime;
t=1/((1/(2*(数学PI)))*Math.sqrt(2*k/m));
系统输出打印ln(t);
dt=t/150;
readfile();

//System.out.println(“第一个:[“+列表粒子[0].粒子位置[0]+”,“+0+””);您可以定义方法来
返回
列表粒子
数组。如下所示:

将其签名更改为:

public static Particle[] readfile()
并在
close
之后添加
return
语句:

return listParticles;
main
中,可以调用此函数并将其返回值指定给变量:

myParticleArray = Particle.readfile();

每个粒子都有一个ID、类型,x、y和z代表位置、速度和力。这是七个变量,但我在你的文件中看到了11个变量?因此,将listParticles声明为类的数据成员,然后公开一个getter方法。@Riley Carney x、y和z代表每个位置、速度和力,而不仅仅是位置在我更改s时不起作用0,我添加了返回和主调用。这是另一个你应该处理的错误……也许考虑用一个完整的错误和堆栈跟踪来发布一个新的问题。发送一个链接给它,我会尽力帮助你。为了帮助大家,这里是我的新问题