zoukankan      html  css  js  c++  java
  • 可见参数和增强for以及自动拆装箱


    可变参数:定义方法的时候不知道该定义多少个参数
    格式:
     修饰符 返回值类型 方法名(数据类型… 变量名){

     }
     注意:
     这里的变量其实是一个数组
    如果一个方法有可变参数,并且有多个参数,那么,可变参数肯定是最后一个

    package newjdk5;
    //增强for 即 for each遍历功能对数组集合遍历
    public class testCanshu {
    public static void main(String[] args) {
    	System.out.println(sum(1,2,3,4));
    	
    }
    
    public static int sum(int... a){
    	int s=0;
    	for(int temp:a){
    		s+=temp;
    	}
    	return s;
    }
    
    public static int sum1(int b, int... a){
    	return b;
    	
    }
    /*public static int sum(int... a,int b){ error
    	return b;
    	
    }*/
    
    }
    

      

    package chaizhuangxiang;
    
    public class testchaizhuang {
    
    	public static void main(String[] args) {
    		/*自动装箱就是Java自动将原始类型值转换成对应的对象,
    		 * 比如将int的变量转换成Integer对象,
    		 * 这个过程叫做装箱,反之将Integer对象转换成int类型值,这个过程叫做拆箱。
    		 * 因为这里的装箱和拆箱是自动进行的非人为转换,所以就称作为自动装箱和拆箱.*/
    		Integer b=10;
    		//自动装箱
    		int a=new Integer(10);
    		//自动拆箱
    
    	}
    
    }
    

      

    package chaizhuangxiang;
    //String,Integer,Character之间转换
    public class test {
    public static void main(String[] args) {
    	String a="李云龙-42-团长";
    	//截取年龄
    	String age=a.substring(4, 6);
    	
    	System.out.println(age);
    	//将字符串转为Integer类型
    	Integer age1=Integer.valueOf(age);
    	//自动装箱
    	int age2=age1;
    	//强转为char
    	char age3=(char)age2;
    	//char类型 转为Character
    	Character age4=Character.valueOf(age3);
    	//将character转为int类型
    	int age7= age4;
    	//Character转为字符串
    	String age5=""+age7;
    	System.out.println(age5);
    	String age6=String.valueOf(age7);
    	System.out.println(age6);
    	
    	
    }
    }
    

      

  • 相关阅读:
    Floyd_Warshall算法
    Bellman_Ford算法
    深度优先搜索
    广度优先搜索
    贪心算法_活动选择
    动态规划_0-1背包问题
    算法导论_动态规划_最长回文子序列
    算法导论_动态规划_最长公共子序列
    动态规划解决分割问题
    2016 Google中国开发者大会游记
  • 原文地址:https://www.cnblogs.com/ysg520/p/9568450.html
Copyright © 2011-2022 走看看