2

If you try to do 15/2 it will return 7 because it thinks that 15 and 2 are integers. If you do (double)15/2 it will return 7.5 because it knows that 15 is a double.

Say you have a function named test() that takes in an array as an argument. How would you pass an array into the function without defining it first?

I have tried doing something like this:

test((int[]){1,2,3,4})

2 Answers 2

4

You're pretty close. You can do it like this:

test(new int[]{1, 2, 3, 4});
Sign up to request clarification or add additional context in comments.

Comments

2

I think you are looking for a variadic function (aka varargs), something like

public static double test(int... vals) {
    return IntStream.of(vals).sum() / (double) vals.length;
}

can be called with any number of int(s). For example,

System.out.println(test(1, 2, 3));

Which outputs

2.0

Comments

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.