Java 16-13 variable parameters and Aslist () method for arrays tool classes

Source: Internet
Author: User


Mutable parameters: When defining a method, you do not know how many parameters to define
Format:
Modifier returns a value type method name (data type ... Variable Name) {

}

Attention:
The variable here is actually an array
If a method has a variable parameter, and there are multiple parameters, then the variable parameter is definitely the last

1 Importjava.util.Scanner;2  public classArgsdemo {3 4  public Static voidmain (string[] Args) {5 intresult = Sum (1,2,3,4,5,6,7,8,9);//The number of numbers participating in the calculation can be changed arbitrarily6 System.out.println (result);7 }8 9 //An addition method that defines a parameter as a variable parameterTen  public Static intSumint.. a) {//the variable here is actually an array one ints = 0; a  for(intI:a) { -s + =i; - } the returns; - } -  -}


public static <T> list<t> aslist (T ... a): convert arrays to Collections

Precautions:
Although you can turn an array into a collection, the length of the collection cannot be Changed.

1  packagecn.itcast_03;2 3 Importjava.util.Arrays;4 Importjava.util.List;5 6 7  public classArraysdemo {8  public Static voidmain (string[] Args) {9 //define an arrayTen //string[] strarray = {"hello", "world", "java"}; one //list<string> List = arrays.aslist (strarray); a  -list<string> list = arrays.aslist ("hello", "world", "java"); - //unsupportedoperationexception the //list.add ("java ee");//cannot increase the number of elements - //unsupportedoperationexception - //List.remove (1);//cannot reduce the number of elements -  +List.set (1, "java ee");//without changing the length of the collection, you can -  +  for(String S:list) { a System.out.println (s); at } - } - } -  -  

Java 16-13 variable parameters and Aslist () method for arrays tool classes

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.