zoukankan      html  css  js  c++  java
  • Day05_java方法 可变参数

    可变参数(不定项参数)

    • JDK 1.5开始,Java支持传递同类型的可变参数给一个方法。

    • 在方法声明中,在指定参数类型后加一个省略号(.…)。

    • 一个方法中只能指定一个可变参数,它必须是方法的最后一个参数。任何普通的参数必须在它之前的声明。

      public static void printMax( double...numbers) {
      	if (numbers.length == 0) {
      		System.out.println( "No argument passed" );
      		return;
      	}
      	double result = numbers[0];
      	//排序
      	for (int i = 1; i <numbers.length; i++){
      		if ( numbers[i] >result) {
      			result = numbers[i];
      		}
      	}
      	System.out.println( "The max value is " + result);
      }
      
    package com.lemon.method;
    
    public class Demo04 {
        public static void main(String[] args) {
            //调用可变参数的方法
            printMax(232,323,123,1,34,12);
            printMax(new double[]{1,2,43});
    
        }
        public static void printMax( double...numbers) {
            if (numbers.length == 0) {
                System.out.println( "No argument passed" );
                return;
            }
            double result = numbers[0];
            //排序
            for (int i = 1; i <numbers.length; i++){
                if ( numbers[i] >result) {
                    result = numbers[i];
                }
            }
            System.out.println( "The max value is " + result);
        }
    }
    
    //运行结果
    The max value is 323.0
    The max value is 43.0
    
    Process finished with exit code 0
    
  • 相关阅读:
    C++调用外部应用程序
    SVN文件加锁
    vs ComboBox显示多行
    __slots__ Python Class限制添加属性
    Python数据分析之pandas学习
    整理Lua和Unity和Lua交互文章链接
    [整理]Unity3D游戏开发之Lua
    ping telnet ssh netstat
    java rpc
    css 手机适配
  • 原文地址:https://www.cnblogs.com/lemonlover/p/14002091.html
Copyright © 2011-2022 走看看