从另一个类Java更新数组

时间:2014-02-11 09:10:52

标签: java arrays class

好吧,我还是很陌生。所以这个想法是我想从一个类中获取一个数组,通过一些方程发送它,然后输出更新的版本。我还需要它迭代几次。我最好的猜测是在我的主类中设置一个while循环,其中的class方法对应于数组更新程序。它只是吐出相同的值(没有更新)我真的很感激任何关于我可能做错的建议或提示。

**如果您打算告诉我读书或上课,请继续。我正在自学,这个网站只有在正确使用时才非常有用。感谢

代码很长,所以我会尝试发布我认为最有帮助的代码段。

**此部分包含原始数组(rad [],vol [] ...)我想更新

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) 

{}

**这个类应该更新数组。抱歉长度

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 );
    }

3 个答案:

答案 0 :(得分:2)

在Java中要记住的一件事是原始类型是按值传递的,而对于对象,对象的引用是按值传递的。

如果将基元传递给方法,并在从该方法返回时在该方法内更改它,则初始值不会更改,因为您只在方法中更改了它的副本。

要从方法中获取新值,必须从方法返回值并将旧值设置为方法的返回值。

如果您有多个需要返回的值,则无法在Java中执行此操作,因为方法可能只有一个返回值。

你可以做的一件事就是使用[]数组,而不是使用Array对象并将它们作为参数传递。当您在编辑相同的基础数据对象时从方法返回时,您在方法内所做的任何更改都会反映出来。 (请注意,如果重新分配此数组对象,例如inputArrayObject = new ArrayList();,则在方法内部将丢失对外部对象的引用,并且当您从方法返回时,将不再反映任何后续更改。

编辑:正如chrylis所说,看起来你的那些数组可能是密切相关的,应该被拉入一个类,然后你可以将该类的一个对象传递给一个能够正确改变数值的方法,或者根据确切需要完成的操作,在对象上调用类方法。

Edit2:不够高的Rep对问题发表评论所以我会把它放在这里。现在您已经发布了代码,我看到您使用所有参数做的第一件事是将它们传递给InitialGrip构造函数,为什么不在外部构造该对象并将其传递给Iterator构造函数,它会更清晰。此外,您应该尝试将您正在进行的一些数学运算分解为方法。因此,而不是localVar =(一吨数学)有localVar = calucalteDensity(参数)。它会使它更容易阅读和调试,并且可以让你重复使用你需要的任何东西(我没有直接进入代码以确切知道它正在做什么,所以也许没有什么可以重用,一切都可能不同)

答案 1 :(得分:0)

我不知道我是否确切地知道了你的问题,但是如果你想将一个数组传递给一个方法,你可以简单地传递它并设置一个与你的方法输出相同类型的数组;就像这样:

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

答案 2 :(得分:0)

尝试将数组定义为静态变量,以使其全局起作用。这样,在函数中对该方法所做的任何更改都会影响该变量的整体值。在这种情况下:

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

可以通过调用mySample.rad进行访问,并且在任何函数中对其进行的更改也将与另一个类中的变量调用一起出现。