尝试在不同方法中使用变量时出错

时间:2016-01-31 23:19:02

标签: java arrays methods

在关注我的上一个问题HERE的建议之后,我遇到了一个异常问题:java.lang.ArrayIndexOutOfBoundsException

我的程序从文件中读取(当前)两个粒子及其当前位置,速度和力(所有三个属性的x,y和z)的数据。这是我的档案:

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,类型和位置,速度和力。

我要做的是在readfile()中使用main的局部变量,并尝试修复我之前的问题。

这是我的代码:

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();
    Particle [] mainParticles = Particle.readfile();
    System.out.println("First:  [ " + mainParticles[0].particlePosition[0] + " , " + 0 + " ]");
    System.out.println("Second:  [ " + mainParticles[1].particlePosition[0] + " , " + 0 + " ]");

}

public static Particle[] readfile(){

        Particle [] listParticles = new Particle[count];

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

        count = readData.nextInt();

        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();
        return listParticles;
    }
}

2 个答案:

答案 0 :(得分:1)

在黑暗中拍摄,但明显的错误是你如何初始化Particle[]数组列表。您应该在设置count后执行此操作,否则arraysize将为0。

    count = readData.nextInt();
    Particle [] listParticles = new Particle[count];

答案 1 :(得分:1)

readFile()方法中,您尝试在从文件中读取count之前创建一个大小为count的数组,因此它始终会创建一个大小为0的数组(默认值)类型int)的值

更改以下部分

  Particle [] listParticles = new Particle[count];

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

    count = readData.nextInt();

    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];

在第二个代码块中,在创建数组之前设置变量count,因此它创建了适当大小的数组