Java basis — variable length parameters

1. length is not fixed, it can be transmitted more./

> variable length parameter.

Examples:

//public void print(String…param){

for(String str:param){

System.out.println(str);

}

}

 

Give an example:

public class Demo {

public void p(int…param){

for (int i = 0; i < param.length; i++) {

System.out.println(param[i]+”\t”);

}

System.out.println();

System.out.println(“**************************”);

}

}

 

import static org.junit.Assert.*;

import org.junit.Test;

public class DemoTest {

@Test

public void test() {

Demo d=new Demo();

d.p(‘a’);

d.p(‘a’,’b’);

d.p(‘a’,’b’,’c’);

}

}

2.When variable length calls are used, you can give any parameter or no parameter.

3.1Give an example:

public void p(String…param) {

}

Public void p(int a,int b){

}

2) If the method to be invoked can match two variable parameters, an error occurs.

Give an example:

public void p(String…param,String…param) {

}

3) A method can only have one variable-length parameter, and this variable-length parameter must be the last parameter of the method

Give an example:

Error public void P (String… Param),int a) {    }

Correct public void P (int a),String…param) {    }

 

Leave a Reply

Your email address will not be published. Required fields are marked *