-1

I have an 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?

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;
        }
}
1
  • Iterate throught the list to get MSS_Vector_Alg and for each of them add their x,y,z Commented Feb 13, 2015 at 0:20

2 Answers 2

3

Most elegant solution would probably be to use 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();
Sign up to request clarification or add additional context in comments.

Comments

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 Comments

Fernando, you know that '-' cannot be part of a java identifier, don't you?
yeah, I used it for some resemblance with his variables hahah (already removed the '_' in the class name just to get it colored)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.