-1

I was learning Transitions from following article:Transitions - ADP.

He has implemented method toggleVisibility() as follows:

 public void onClick(View v) {
        TransitionManager.beginDelayedTransition(mRootView, new Fade());
        toggleVisibility(mRedBox, mGreenBox, mBlueBox, mBlackBox);
    }

    private static void toggleVisibility(View... views) {
        for (View view : views) {
            boolean isVisible = view.getVisibility() == View.VISIBLE;
            view.setVisibility(isVisible ? View.INVISIBLE : View.VISIBLE);
        }

From above I understand that View... views represents all view parameters in the method. But, I have never seen before this ... operator. What is it? How it works? I googled nut couldn't get any anser. Can anyone help me?

xyz
  • 1,325
  • 5
  • 26
  • 50

3 Answers3

4

... is called varags. It allows you to pass any number of Views (0 or more) to that method.

You can call it, for example with:

toggleVisibility(); // no views
toggleVisibility(view1); // one view
toggleVisibility(view1,view2); // two views
...

It's equivalent to passing a View[] array to a method.

When you write a method call, the compiler matches the parameters passed to that method call to the agrument list of all the methods having the same name in order to find the best fitting method to call. Zero or more instances of View (all of which must be the last parameters in the method call) can be matched to a single View... argument in the method definition (which must be the last parameter). Inside the method, you can treat the View... variable as a variable of View[] type (i.e. an array).

A varargs parameter has the lowest precedence when determining the most suitable overloaded method to be called. Therefore, if you overload toggleVisibility(), it will compile, and calling toggleVisibility() without any arguments will invoke the method that has no arguments. Similarly, you can declare toggleVisibility(View v) which will have precedence over the varargs version, but the point in varargs is to save you the need to define multiple methods with the same name and the same type of parameters that only differ in the number of parameters of that same type.

Eran
  • 387,369
  • 54
  • 702
  • 768
  • thanks...but How does it work? I mean method has multiple parameters and implemented has only one! – xyz Jan 01 '15 at 11:43
  • But what if I overload method toggleVisibility()? Will it result in error? – xyz Jan 01 '15 at 12:27
3

View... views is a variadic parameter. It means you can pass any number of views to it, separated by commas as if they were separate arguments. The function can then treat that parameter views as a collection with all of the arguments passed in inside of it.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
  • 1
    Did you mean varargs? – xyz Jan 01 '15 at 11:40
  • 1
    Yes. Varargs is short for variadic arguments. – Gabe Sechan Jan 01 '15 at 11:41
  • thanks...but How does it work? I mean method has multiple parameters and implemented has only one! – – xyz Jan 01 '15 at 11:45
  • The compiler behind the scenes creates a List where X is the type of the variable. It passes that List in as the single parameter. When you write the implementation, you treat that single parameter as a List. – Gabe Sechan Jan 01 '15 at 11:47
  • thanks..I'll accept it..But please move these all above information in comments to answer. – xyz Jan 01 '15 at 11:48
0

this is varagas, it allows you to pass zero or multiple arguments to a function. It allows you to avoid using overloaded version of your function or using array input(a common workaround) varagas has 2 rules, varagas are also called variable arguments

1- only one variable argument is allowed per function

2- Variable argument should be the last argument

void doSomething1(String... values, int x){} gives a compile time error

void doSomething2(String... values, int... vals) gives a compile time error

for more info,view Varagas Docs

Basil
  • 845
  • 9
  • 24
  • I din't seen your first rule is folowd here: http://viralpatel.net/blogs/varargs-in-java-variable-argument-method-in-java-5/ – xyz Jan 01 '15 at 12:30