[英]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.