简体   繁体   English

从另一个类Java更新数组

[英]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. 我对此的最佳猜测是在我的主类中设置一个while循环,其内部的类方法与数组更新程序相对应。 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 **这部分包含我想更新的原始数组(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) 

{}

**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)) ; Math.pow(x,4)+ 7.0 *(1.0-2 * z)* Math.pow(x,2)+ 5 *(z-1.8));
x = x - ( dy / dydx ); 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. 在Java中要记住的一件事是,原始类型是通过值传递的,而使用对象时,对对象的引用是通过值传递的。

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. 如果您有多个需要返回的值,则在Java中无法执行此操作,因为一种方法可能只有一个返回值。

One thing you could do is rather than use [] arrays you could use Array objects and pass these in as arguments. 您可以做的一件事是可以使用Array对象并将其作为参数传递,而不是使用[]数组。 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. (但是请注意,如果重新分配此数组对象,例如inputArrayObject = new ArrayList();),则在方法内部将丢失对外部对象的引用,并且从该方法返回时,以后的任何更改都将不再反映。

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. 编辑:正如chrylis所说,看起来您的那些数组可能密切相关,应该将其拉到一个类中,然后您可以将该类的对象传递到可以正确更改值或调用一个类的方法中对象上的方法取决于实际需要执行的操作。

Edit2: Not high enough Rep to comment on question so I'll put this here. Edit2:代表不足以对问题进行评论,所以我将其放在此处。 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. 现在,您已经发布了代码,我看到对所有参数进行的第一件事就是将它们传递给InitialGrip构造函数,为什么不从外部构造该对象并将其传递给Iterator构造函数,这样会更清洁。 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). 因此,不是让localVar =(大量的数学运算)而是让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. 可以通过调用mySample.rad进行访问,并且在任何函数中对其所做的更改也将随另一个类中的变量调用一起提供。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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