简体   繁体   中英

Updating an array from another class Java

Alright I am still pretty new to this. So the idea is I would like to take an array from one class, send it through some equations and then output an updated version. I also need it to iterate a few times. My best guess at this was to setup a while loop in my main class with the class method inside of it that corresponds to the array updater. Its just spitting out the same values (not updated) I would really appreciate any suggestions or tips on what I might be doing wrong.

**If you are going to tell me to read a book or take a class, please move on. I'm learning on my own and this site is extremely helpful only when it is used correctly. Thanks

The code is really long so I will try to post what snippets I feel will be most helpful.

** This portion contains the original arrays (rad[ ] , vol[ ]...) I would like to update

public class InitialGrids {


PlanMat pm = new PlanMat();

int iic,ioc,iman ;
int nic, noc;
int iccomp, occomp, mcomp;
double masIC, masOC, masMAN;
double volp;
double radIC, radOC, radp;


public int iMat[ ];
public double rad[ ];
public double vol[ ];
public double rho0[ ];
public double mas[ ];
public double dmas[ ];

double pi = Math.PI;

public InitialGrids(int icMat, int ocMat, int manMat, int shells, double massP, double ficore, double focore, double fman) 

{}

**This class is supposed to update the array. sorry for the length

public class Iterator 

  {
final double G;
final double PI;
double Pave;
double x, z, dy, dz, dydx; // this is for the iteration that will update the density
double delta;

public double grav[ ];
public double P[ ];
public double rho0n[ ];

int mix;



public Iterator(int icMat, int ocMat, int manMat, int shells, double massP, double ficore, 

      double focore, double fman) 
{
    InitialGrids ig = new InitialGrids(icMat, ocMat, manMat, shells, massP, ficore, 
       focore, fman);
    Constants c = new Constants();
    PlanMat pm = new PlanMat();

    G = c.GC;
    PI = c.PI;

    // calculation of gravity force at each shell

    grav = new double [ shells + 1 ];

    for(int k = 0; k <= shells; k++)
    {
        grav[ k ] = ( (G*ig.mas[k]) / (Math.pow(ig.rad[k], 2)) );   
    }

    // calculation of pressure at zone boundaries

    P = new double [ shells + 1 ];

    for(int k = shells - 1; k >= 0; k-- )
    {
        P[shells] = 0;

        P[ k ] = 0.5 * (grav[ k + 1 ] + grav[ k ]) * (ig.rad[ k + 1] - ig.rad[ k ]) *
            ig.rho0[ k + 1 ];

    }

    // this will calculate for the average pressure grid

    rho0n = new double[ shells + 1 ];

    for(int k = 1; k <= shells; k++)
    {
        rho0n[ 0 ] = 0;
        Pave = 0.5 * (P[ k ] + P[ k - 1 ]);

        if(pm.eos[ ig.iMat[ k ] ] == 1)
        {
            z = 0.75*(pm.Ksp[ ig.iMat[ k ] ] - 4.0);
            x = 1.0;
            for(int j = 1; j <= 20; j++)
            {
                dy = 1.5 * (pm.Ks0[ ig.iMat[k] ]) * Math.pow(x, 5) *
                                     (z * Math.pow(x,4) + (1.0 - 2.0 * z) * Math.pow(x, 2) + (z - 1.0)) - Pave ;
                dydx = 1.5 * (pm.Ks0[ ig.iMat[ k ] ]) * Math.pow(x, 4) *
                                     (9.0 * z * 

Math.pow(x, 4) + 7.0 * (1.0 - 2*z) * Math.pow(x, 2) + 5 * (z - 1.8)) ;
x = x - ( dy / dydx ); }

            rho0n[ k ] = ig.rho0[ k ] * Math.pow(x, 3);             
        }

        else
        {
            rho0n[ k ] = pm.c[ ig.iMat[k] ] * Math.pow(Pave , pm.nn[ ig.iMat[k] ]);
            rho0n[ k ] = rho0n[ k ] + pm.rho0[ ig.iMat[k] ] ;
        }

    }

    // The following will: define the change in density after iterations, mix the old and new densities and then update the radial grids

    delta = (Math.abs( rho0n[1] / ig.rho0[1] ) - 1);

    mix = 1;

    for(int k = 0; k <= shells; k++)
    {
        rho0n[ k ] = rho0n[ k ] + ig.rho0[ k ] * (1 - mix);             //notice that rho0 = rho in desch's code. dont worry
    }

    // radius update using density dependent volume and then calculating radius from volume

    for(int k = 1; k <= shells; k++)
    {
        ig.rad[ 0 ] = 0;
        ig.vol[ 0 ] = 0;

        ig.vol[ k ] = ( ig.dmas[ k ] / ig.rho0[ k ] );
        ig.rad[ k ] = Math.pow( ( 0.75 * ig.dmas[ k ] / ig.rad[ k ] / PI + Math.pow(ig.rad[ k - 1 ], 3) ) , 1.0 / 3 );
    }

One thing to remember in Java is that primitive types are passed by value whereas with Objects the reference to the object is passed by value.

If you pass a primitive into a method, and change it inside that method when you return from that method the initial value will not have changed as you have only changed a copy of it inside the method.

To get a new value out of the method you must return the value from the method and set the old value to the return of the method.

If you have multiple values that need to be returned you can't do this in Java, as a method may only have one return value.

One thing you could do is rather than use [] arrays you could use Array objects and pass these in as arguments. THen any changes you do inside the method will be reflected when you return from the method as you were editing the same underlying data object. (Note though that if you reassign this array objects, like inputArrayObject = new ArrayList(); , inside the method you will lose the reference to the external object and any later changes will no longer be reflected when you return from the method.

Edit: as chrylis said, it looks like those arrays of your are probably closely related and should maybe be pulled into a class, which you could then pass an object of that class into a method which would properly change the values, or call a class method on the object depending on what exactly needs to be done.

Edit2: Not high enough Rep to comment on question so I'll put this here. Now that you have posted the code I see the first thing you do with all the arguments is pass them into an InitialGrip constructor, why not construct that object externally and pass that into the Iterator constructor, it would be cleaner. Also you should try and break some of those mathematical operations you are doing into methods. So rather than have localVar = (a ton of math) have localVar = calucalteDensity(arguments). It will make it a little easier to read and debug and will let you reuse anything you need to (I didn't get right into the code to know exactly what it's doing so maybe nothing can be reused, everything might be different)

I don't know if I've got your question exactly, however if you want to pass an array to a method you can simply pass it and set an array of the same type as the output of your method;like this:

int[] methodNae(int iMat[ ]) {
    int[] result;// you can even change the iMat and send it as the result
    //Do whatever you want with iMat
    return result;
}

Try defining your array as a static variable so that it acts globally. That way, any change made to that method in a function will affect the overall value of that variable. In this case:

public class mySample{
public static double[] rad = {};
}

Can be accessed by calling mySample.rad and the changes made to it in any function will also come with the variable call in another class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM