我有一个ArrayList:
ArrayList<MSS_Vector_Alg> vectors = new ArrayList<MSS_Vector_Alg>();
它将包含
之类的对象MSS_Vector_Alg(float x, float y, float z)
正如您所看到的,它具有(x,y,z)
值。我希望添加所有x
值,y
值和z
值,以便获得(x-total,y-total,z-total)。有没有办法可以做到这一点?
代码(供参考的课程):
MSS_Vector_Alg:
public class MSS_Vector_Alg {
float x;
float y;
float z;
int dimension;
String unit;
//basic ... assumes a 3D vector will be used
public MSS_Vector_Alg(){
this.x = 0;
this.y = 0;
this.z = 0;
this.dimension = 3;
this.unit = "unit";
}
//3D vector constructor
public MSS_Vector_Alg(float x, float y, float z){
this.x = x;
this.y = y;
this.z = z;
this.dimension = 3;
this.unit = "unit";
}
//2D vector constructor
public MSS_Vector_Alg (float x, float y){
this.x = x;
this.y = y;
this.dimension = 2;
this.unit = "unit";
}
//1D vector constructor
public MSS_Vector_Alg(float x){
this.x = x;
this.dimension = 1;
this.unit = "unit";
}
// getter and setters
}
MSS_Vector_Math:
public final class MSS_Vector_Math {
// a list of possible method for various vector operations
//addition
public static MSS_Vector_Alg add(ArrayList<MSS_Vector_Alg> vectors){
//TODO
return null;
}
//opposite
public static MSS_Vector_Alg opposite(MSS_Vector_Alg vector){
float tempx;
float tempy;
float tempz;
tempx = -vector.getx();
tempy = -vector.gety();
tempz = -vector.getz();
MSS_Vector_Alg rev = new MSS_Vector_Alg(tempx, tempy, tempz);
return rev;
}
//scalar multiplication
public static MSS_Vector_Alg scalarMultiply(MSS_Vector_Alg vector, float scalar){
float scax;
float scay;
float scaz;
scax = vector.getx() * scalar;
scay = vector.gety() * scalar;
scaz = vector.getz() * scalar;
MSS_Vector_Alg smul = new MSS_Vector_Alg(scax, scay, scaz);
return smul;
}
//dot multiply
public static Float dotMultiply (MSS_Vector_Alg vector1,MSS_Vector_Alg vector2 ){
float dotx;
float doty;
float dotz;
float dotmul;
dotx = vector1.getx()*vector2.getx();
doty = vector1.gety()*vector2.gety();
dotz = vector1.getz()*vector2.getz();
dotmul = dotx + doty + dotz;
return dotmul;
}
//cross multiply
public static MSS_Vector_Alg crossMultiply(MSS_Vector_Alg vector1,MSS_Vector_Alg vector2){
float x1, y1, z1, x2, y2, z2, crossx, crossy, crossz;
x1 = vector1.getx();
y1 = vector1.gety();
z1 = vector1.getz();
x2 = vector2.getx();
y2 = vector2.gety();
z2 = vector2.getz();
crossx = (y1*z2)-(z1*y2);
crossy = (z1*x2)-(x1*z2);
crossz = (x1*y2)-(y1*x2);
MSS_Vector_Alg crsmul = new MSS_Vector_Alg(crossx, crossy, crossz);
return crsmul;
}
//convert from polar to algebraic
public static MSS_Vector_Alg convertPolarToAlgebraic(MSS_Vector_Pol polarVector){
float conx, cony, conz, r;
r = polarVector.getMagnitude();
conx = ((float)r * (float)Math.cos(polarVector.getAlpha()));
cony = ((float)r * (float)Math.cos(polarVector.getBeta()));
conz = ((float)r * (float)Math.cos(polarVector.getGamma()));
MSS_Vector_Alg conv = new MSS_Vector_Alg(conx, cony, conz);
return conv;
}
//convert from algebraic to polar
public static MSS_Vector_Pol convertAlgebraicToPolar(MSS_Vector_Alg algVector){
float conx, cony, conz, r, alpha, beta, gamma;
conx = algVector.getx();
cony = algVector.gety();
conz = algVector.getz();
r = (float)Math.sqrt((conx*conx + cony*cony + conz*conz));
alpha = (float)Math.acos(conx/r);
beta = (float)Math.acos(cony/r);
gamma = (float)Math.acos(conz/r);
MSS_Vector_Pol polvec = new MSS_Vector_Pol(r, alpha, beta, gamma);
return polvec;
}
//check validity of directional angles in 3D
public static boolean checkAngles3D(float alpha, float beta, float gamma){
//TODO
return true;
}
//check validity of directional angle in 2D
public static boolean checkAngle2D(float alpha){
//TODO
return true;
}
//check validity of directional angle in 1D
public static boolean checkAngle1D(float alpha){
//TODO
return true;
}
}
答案 0 :(得分:3)
最优雅的解决方案可能是使用Java Stream API:
List<MSS_Vector_Alg> vectors = new ArrayList<MSS_Vector_Alg>();
float sumOfXs = (float) vectors.stream().mapToDouble(mss -> mss.x).sum();
float sumOfYs = (float) vectors.stream().mapToDouble(mss -> mss.y).sum();
float sumOfZs = (float) vectors.stream().mapToDouble(mss -> mss.z).sum();
答案 1 :(得分:1)
您是否尝试过对列表进行迭代并添加这些数字?
List<MSSVectorAlg> vectors = new ArrayList<MSSVectorAlg>();
int xTotal = 0, yTotal = 0, zTotal = 0;
for (MSSVectorAlg vector : vectors)
{
xTotal += vector.getX();
yTotal += vector.getY();
zTotal += vector.getZ();
}