如何在ArrayList(Java)中找到值的总和? [重复]

时间:2022-04-16 12:03:00

This question already has an answer here:

这个问题在这里已有答案:

I have an ArrayList:

我有一个ArrayList:

ArrayList<MSS_Vector_Alg> vectors = new ArrayList<MSS_Vector_Alg>();

It will contain objects like

它将包含像

MSS_Vector_Alg(float x, float y, float z)

as you can see it has an (x,y,z) value. I am looking to add all the x values, y values and z values so I can get a (x-total, y-total, z-total). Is there a way I can do that?

你可以看到它有一个(x,y,z)值。我希望添加所有x值,y值和z值,这样我就可以得到(x-total,y-total,z-total)。有没有办法可以做到这一点?

Code ( classes for reference ):

代码(参考类):

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

2 个解决方案

#1


1  

Have you tried iterating over the list and adding those numbers?

您是否尝试过迭代列表并添加这些数字?

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

#2


3  

Most elegant solution would probably be to use Java Stream API:

最优雅的解决方案可能是使用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  

Have you tried iterating over the list and adding those numbers?

您是否尝试过迭代列表并添加这些数字?

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

#2


3  

Most elegant solution would probably be to use Java Stream API:

最优雅的解决方案可能是使用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();