2
public static void testArray(int[]a)
{
    for(int i:a)
    {

        if(i==10)
        {
        System.out.println("Number 10 exists !");
        }
    }
}

I want to pass parameters to this method without creating variables. I want to call it like so:

testArray({5, 10, 15});

Is this possible?

3 Answers 3

8
testArray(new int[]{5, 10, 15});
Sign up to request clarification or add additional context in comments.

Comments

3
public static void testArray(int ... a)
{ 
    for(int i:a)
        if(i==10)
            System.out.println("Number 10 exists !");
}

testArray(5, 10, 15);

3 Comments

Whaaat !? "int ... a" are you sure?
This answer should point out that this is only viable for the last method argument.
1
testArray(new int[] {1,2,3,4});

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.