问题
After following the advice on my last question HERE , I ran into the problem with an exception: java.lang.ArrayIndexOutOfBoundsException
My program reads data from a file about (currently) two particles and their current position, velocity, and force (with an x,y, and z for all three attributes). Here is my file:
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
The first number is count, how many particles there are. Each particle has an ID, type, and position, velocity, and force.
What I am trying to do is to use the local variable of readfile() in main, and attempted to fix my previous problem.
Here is my code:
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;
}
}
回答1:
In your readFile() method, you are trying to create an array of size count before count has been read from the file, so it always creates an array of size 0 (Default value of type int)
Change below part
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();
to
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];
In the 2nd code block variable count is set before creating array, and so it creates array of appropriate size
回答2:
Shot in the dark but an obvious error is how you're initializing the Particle[] array list. You should do this after you set count, otherwise the arraysize will be 0.
count = readData.nextInt();
Particle [] listParticles = new Particle[count];
来源:https://stackoverflow.com/questions/35120747/error-when-trying-to-use-variable-in-different-method