0

I'm over-engineering a photo project right now and trying to make it really easy to add more images with filters to a collage of images. I have a 2D array of all the pictures, a 2D array of all the methods to call, and a 3D array of Objects. I went with a 3D array of objects to represent the parameters since each method has a differing number of parameters and different types (i.e. int, Color, etc). Right now, I am able to pass the parameters but it is hard-capped at three, and to increase that number I have to hard code in more if statements. My code is as follows:

for (int row = 0; row < pictures.length; row++)
        {
            for (int col = 0; col < pictures[row].length; col++)
            {
                Picture current = pictures[row][col];
                Method m = methods[row][col];
                Object[] p = parameters[row][col];
                
                int numOfParameters = p.length;
                
                if (numOfParameters == 0)
                    m.invoke(current);
                else if (numOfParameters == 1)
                    m.invoke(current, p[0]);
                else if (numOfParameters == 2)
                    m.invoke(current, p[0], p[1]);
                else if (numOfParameters == 3)
                    m.invoke(current, p[0], p[1], p[2]);
                
                canvas.cropAndCopy(current, 0, height, 0, width, height * row, width * col);
            }
        }

Would it be possible to do something like

m.invoke(currentPicture, unpack(p));

In such a way that it is equal to this

m.invoke(currentPicture);

this,

m.invoke(currentPicture, param1);

this,

m.invoke(currentPicture, param1, param2);

etc. depending on the size of the array or something so that it automatically adjusts to the number of parameters?

I have searched essentially everywhere to find an answer, but the only thing I'm seeing is varargs, which isn't what I'm looking for, I need a way to send one parameter but "unpack" it into n number of parameters where n represents the size of the array

Creepabob
  • 9
  • 1

0 Answers0