0

Im trying to call this method from SDK

public ThumborUrlBuilder crop(int top, int left, int bottom, int right) {
if (top < 0) {
  throw new IllegalArgumentException("Top must be greater or equal to zero.");
}
if (left < 0) {
  throw new IllegalArgumentException("Left must be greater or equal to zero.");
}
if (bottom < 1 || bottom <= top) {
  throw new IllegalArgumentException("Bottom must be greater than zero and top.");
}
if (right < 1 || right <= left) {
  throw new IllegalArgumentException("Right must be greater than zero and left.");
}
hasCrop = true;
cropTop = top;
cropLeft = left;
cropBottom = bottom;
cropRight = right;
return this;
}

How I can call the method if the parameters are from an Array or Map like this? Is that possible?

ArrayList arrayList = [299, 296, 301, 297]
crop(arraylist)
2
  • No. Your method can accept exact 4 integer values and not array or list of integers. If you wish you can use variable length argument in method and pass an array with 4 values. But that's​ I think you should not do because it will allow to call method with no or any number of argument. Commented May 15, 2017 at 3:11
  • Yes you can in groovy. check my answer. Commented May 15, 2017 at 6:31

3 Answers 3

1

Java:

No you cant.

you will get this error:

Compilation Errors Detected
...
method crop in class Test cannot be applied to given types;
  required: int,int,int,int
  found: java.util.ArrayList<java.lang.Integer>
  reason: actual and formal argument lists differ in length

Groovy:

Yes you can. Check the sample code on groovyConsole.

def hello(int a, int b){ 
    println "$a and $b" 
}

hello(1, 2)

def param = [1,2]
hello(param)
Sign up to request clarification or add additional context in comments.

Comments

1
public ThumborUrlBuilder crop(ArrayList params) {
    if (params.size() != 4 ){
       throw new IllegalArgumentException(...);
    }
    int top = params.get(0);
    int left = params.get(1);
    int bottom = params.get(2);
    int right = params.get(3);
    ...
}

1 Comment

This is not safe, because list with less than 4 elements can raise exception and if list has more than 4 elements other elements will be ignored.
0

This is not directly possible in Java, because the function crop requires 4 parameters.

Passing the given ArrayList into the crop function would result in an error.

You could write your own function to handle the ArrayList for you like this:

public ThumboUrlBuilder special_crop(ArrayList arraylist){
    crop(arraylist.get(0),arraylist.get(1),arraylist.get(2),arraylist.get(3));
}

1 Comment

No need to name the method something else. This is a prime example of method overloading, to make the method easier to call, by supplying various "helper" versions for different types of parameters.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.