后续:嵌套循环和数组

时间:2015-12-03 23:12:49

标签: java arrays loops

Using nested loops

我仍在努力实现上述问题中所述的相同内容。基本上我的代码现在看起来像这样:

for(int ac = 0; ac < gravityObject.length; ac++){
        gravField[ac].setVector(0,0);
        System.out.println(ac);
        for(int ad=0;ad<gravityObject.length;ad++){
            System.out.println(ad);
            if(ac!=ad){
                gravField[ac].increaseBy(gravityObject[ac].aDueToGravity(planetMass[ad], position[ad], position[ac]));
            }
            else{
                //do nothing
            }
            gravField[ac].print();
        }
    }

这很烦人,我不明白它为什么会发生!

实施例: 我想计算由于太阳和月亮引起的地球加速度。所以我有double [] planetMass = {earthMass,sunMass,moonMass},一个粒子对象数组Particle [] planets = {earth,sun,moon}以及由于它们经历的引力而产生的加速度数组,这些数组尚未成为计算: PhysicsVector [] gravField = {earthGrav,sunGrav,moonGrav}。

我的循环用于填充此数组。我想求sunGrav和moonGrav来寻找地球的加速度,并将结果存储在earthGrav中,所以在gravField [0]中。然后是earthGrav + moonGrav,这应该存储为gravField [1]。

我的循环正确计算了gravField [0],gravField [1]和gravField [2]的值,然后遗憾地将所有gravField []设置为等于gravField [2]。

这可能是因为我一直将gravField [ac]重置为零。但如果我不这样做,那么我就没有得到正确的价值观。我的主要课程的其余部分(问题肯定是循环):     public static PhysicsVector [] copyArray(PhysicsVector [] a){         int length = a.length;         PhysicsVector [] copy = new PhysicsVector [length];         System.arraycopy(a,0,copy,0,length);         返回副本;     }

public static double sumArray(double[] p){
    double sum = 0;
    for(int z= 0; z < p.length; z++){
        sum += p[z];
    }
    return sum;
}

public static PhysicsVector[] subtract(PhysicsVector[] diff, PhysicsVector g){
    for (int ab=0; ab<diff.length;ab++){ 
        diff[ab].decreaseBy(g);
    }
    return diff;
}   
public static void main(String[] args) throws IOException{

    java.io.File file = new java.io.File("output.txt" );
    java.io.PrintWriter n = new PrintWriter(file);

    //Initialise variables here
    PhysicsVector earthInitialPos = new PhysicsVector();
    PhysicsVector earthInitialV = new PhysicsVector();
    PhysicsVector sunInitialV = new PhysicsVector();
    PhysicsVector sunInitialPos = new PhysicsVector();
    PhysicsVector moonInitialPos = new PhysicsVector();
    PhysicsVector moonInitialV=new PhysicsVector();

    Scanner scanner = new Scanner(System.in);       
    System.out.println("Please enter the size of the time step:");
    double timeStep = scanner.nextDouble();

    //SET PLANETS' INITIAL POSITIONS
    //Set Earth's initial position and initial velocity
    earthInitialPos.setVector(1.4960*Math.pow(10,11),0);
    earthInitialV.setVector(0,29785.5889);
    //Set the sun's initial position
    sunInitialPos.setVector(0,0);
    sunInitialV.setVector(0,-0.08945);      //v = 2*pi*R/T
    //Set the moon's initial position and velocity
    moonInitialPos.setVector(3.844*Math.pow(10,8),0);
    moonInitialPos.increaseBy(earthInitialPos);
    moonInitialV.setVector(0,1023.2195);
    moonInitialV.increaseBy(earthInitialV);

    //CREATE GRAVFIELD OBJECTS
    GravField sunGravField = new GravField(sunMass, sunRadius, sunInitialPos);
    GravField earthGravField = new GravField(earthMass, earthRadius, earthInitialPos);
    GravField moonGravField = new GravField(moonMass, moonRadius, moonInitialPos);

    //CREATE PARTICLE OBJECTS   
    Particle earth = new Particle(earthMass, earthInitialPos, earthInitialV);
    Particle sun = new Particle(sunMass, sunInitialPos, sunInitialV); 
    Particle moon = new Particle(moonMass, moonInitialPos, moonInitialV);

    double time = 0;
    double finalTime = 31557600;            //Run for one earth year

    //Initialise newV and set equal to initialV
    PhysicsVector newV = new PhysicsVector();
    newV = earthInitialV;
    PhysicsVector sunNewV = new PhysicsVector();
    sunNewV = sunInitialV;
    PhysicsVector moonNewV = new PhysicsVector();
    moonNewV = moonInitialV;

    PhysicsVector newSunGrav = new PhysicsVector();
    PhysicsVector newEarthGrav = new PhysicsVector();
    PhysicsVector newMoonGrav = new PhysicsVector();

    GravField[] gravityObject = {earthGravField, sunGravField, moonGravField};
    PhysicsVector[] position = {earthInitialPos, sunInitialPos, moonInitialPos}; 
    PhysicsVector[] velocity = {newV, sunNewV, moonNewV};
    PhysicsVector[] gravField = {zero, zero, zero};
    double[] planetMass = {earthMass, sunMass, moonMass};
    Particle[] planets = {earth, sun, moon};
    PhysicsVector a = new PhysicsVector(0,0);

    //Calculate the centre of mass and subtract position from positions of planets, so c.o.m is at origin
    PhysicsVector centreOfMass = new PhysicsVector();
    centreOfMass = earth.centreOfMass(planetMass, position);
    position = SolarSim.subtract(position, centreOfMass);

    //Calculate centre of mass velocity and subtract from planet velocities
    PhysicsVector centreOfMassVelocity = new PhysicsVector();
    centreOfMassVelocity = earth.cOMVel(planetMass, velocity);
    velocity = SolarSim.subtract(velocity, centreOfMassVelocity);

    //Calculate fields of planets
    for(int ac=0; ac<gravityObject.length; ac++){
        gravField[ac].setVector(0,0);
        System.out.println(ac);
        for(int ad=0;ad<gravityObject.length;ad++){
            System.out.println(ad);
            if(ac!=ad){
                //gravField[ac].increaseBy(gravityObject[ac].aDueToGravity(planetMass[ad], position[ad], position[ac]));
                a.increaseBy(gravityObject[ac].aDueToGravity(planetMass[ad], position[ad], position[ac]));
                gravField[ac] = a;
            }
            else{
                //do nothing
            }
            gravField[ac].print();
        }
    }

    System.out.println(Arrays.toString(gravField));
    PhysicsVector[] newP = new PhysicsVector[position.length];
    PhysicsVector[] newGrav = {zero,zero,zero};
    PhysicsVector[] newVel = new PhysicsVector[velocity.length];

    PhysicsVector sum1 = new PhysicsVector(0,0);
    PhysicsVector sum2 = new PhysicsVector(0,0);
    PhysicsVector sum3 = new PhysicsVector(0,0);

    //do{
        PhysicsVector[] y = new PhysicsVector[gravField.length];
        y=copyArray(gravField);
        PhysicsVector[] z = new PhysicsVector[gravField.length];
        z=copyArray(gravField);

        for(int i=0; i<planets.length;i++){
            sum1.setVector(0,0);    
            for(int s=0;s<z.length;s++){

                if(i!=s){
                    sum1.increaseBy(z[s]); 
                }
                else{
                    sum1.scale(1);  
                }

            }
            newP[i] = planets[i].updatePosition(position[i], velocity[i], timeStep, sum1);
        }
        newP[2].increaseBy(newP[0]);
        //Calculate the centre of mass and subtract position from positions of planets, so c.o.m is at origin
        centreOfMass = earth.centreOfMass(planetMass, newP);
        newP = SolarSim.subtract(newP, centreOfMass);

        for(int j=0; j<gravityObject.length; j++){    

            for(int l=0;l<gravityObject.length;l++){

                if(j!=l){

                newGrav[j].increaseBy(gravityObject[j].aDueToGravity(planetMass[j], position[j], position[l]));
                }
                else{

                }
            }
        }

        for(int k=0; k<planets.length; k++){
            sum2.setVector(0,0);
            sum3.setVector(0,0);
            for(int m = 0; m<y.length;m++){
                if(k!=m){
                    sum2.increaseBy(y[m]);
                    sum3.increaseBy(newGrav[m]);
                }
                else{
                    sum2.scale(1);
                    sum3.scale(1);
                }
            }
            newVel[k] = planets[k].updateVelocity(velocity[k], timeStep, sum2, sum3);
        }
        newVel[2].increaseBy(newVel[0]);
        //Calculate centre of mass velocity and subtract from planet velocities
        centreOfMassVelocity = earth.cOMVel(planetMass, newVel);
        newVel = SolarSim.subtract(newVel, centreOfMassVelocity);

        gravField = copyArray(newGrav);

        time+=timeStep;

        //n.println(e+" "+z);
        //n.println(g+" "+h);

    //}while (time<=finalTime);
    System.out.println(Arrays.toString(newP));

    n.close();
}

}

1 个答案:

答案 0 :(得分:1)

尝试在单独的步骤中清除gravField:

for(int ac = 0; ac < gravityObject.length; ac++){
    gravField[ac].setVector(0,0);
}

for(int ac = 0; ac < gravityObject.length; ac++){
    System.out.println(ac);
    /// the rest of your code as above, without setVector(0,0)