簡體   English   中英

從另一個類Java更新數組

[英]Updating an array from another class Java

好吧,我對此還很陌生。 所以我的想法是,我想從一個類中獲取一個數組,通過一些方程式發送它,然后輸出更新的版本。 我還需要迭代幾次。 我對此的最佳猜測是在我的主類中設置一個while循環,其內部的類方法與數組更新程序相對應。 它只是吐出相同的值(未更新),我真的很感激任何關於我可能做錯事的建議或技巧。

**如果您要告訴我讀書或上課,請繼續。 我自己學習,只有正確使用此站點,該站點才非常有用。 謝謝

代碼很長,因此我將嘗試發布我認為最有用的代碼片段。

**這部分包含我想更新的原始數組(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 );
    }

在Java中要記住的一件事是,原始類型是通過值傳遞的,而使用對象時,對對象的引用是通過值傳遞的。

如果將基元傳遞到方法中,並在從該方法返回時在該方法內部進行更改,則初始值將不會更改,因為您僅在方法內部更改了它的副本。

要從方法中獲取新值,必須從方法中返回值,並將舊值設置為方法的返回值。

如果您有多個需要返回的值,則在Java中無法執行此操作,因為一種方法可能只有一個返回值。

您可以做的一件事是可以使用Array對象並將其作為參數傳遞,而不是使用[]數組。 當您在編輯同一基礎數據對象時從方法中返回時,將反映出您在方法內部所做的任何更改。 (但是請注意,如果重新分配此數組對象,例如inputArrayObject = new ArrayList();),則在方法內部將丟失對外部對象的引用,並且從該方法返回時,以后的任何更改都將不再反映。

編輯:正如chrylis所說,看起來您的那些數組可能密切相關,應該將其拉到一個類中,然后您可以將該類的對象傳遞到可以正確更改值或調用一個類的方法中對象上的方法取決於實際需要執行的操作。

Edit2:代表不足以對問題進行評論,所以我將其放在此處。 現在,您已經發布了代碼,我看到對所有參數進行的第一件事就是將它們傳遞給InitialGrip構造函數,為什么不從外部構造該對象並將其傳遞給Iterator構造函數,這樣會更清潔。 另外,您應該嘗試將正在執行的一些數學運算分解為方法。 因此,不是讓localVar =(大量的數學運算)而是讓localVar = calucalteDensity(arguments)。 這將使它的讀取和調試更加容易,並使您可以重用所需的任何內容(我並沒有完全了解代碼的作用,因此可能無法重用,可能有所不同)。

我不知道我是否確切地提出了您的問題,但是,如果您要將數組傳遞給方法,則可以簡單地將其傳遞並設置與方法的輸出類型相同的數組;就像這樣:

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

嘗試將數組定義為靜態變量,以使其全局起作用。 這樣,在函數中對該方法所做的任何更改都會影響該變量的整體值。 在這種情況下:

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

可以通過調用mySample.rad進行訪問,並且在任何函數中對其所做的更改也將隨另一個類中的變量調用一起提供。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM