zoukankan      html  css  js  c++  java
  • java-3

    学习内容:

    1.数组

    (1)一维数组:

    声明方式:

    复制代码
    int[] a = new int[3];
    a[0] = 1;
    a[1] = 2;
    a[2] = 3;
    
    int[] b = {1,2,3,4};
    
    int[] c = new int[]{1,2,3};
    复制代码

    遍历数组:

    复制代码
    class Test 
    {      int[] a = {1,2,3};
        public static void main(String[] args) 
        {    
                    for(int i = 0;i<a.length;i++){
                        System.out.println(a[i]);
              }         
              for(int each : a){ //增强型for循环,类似js的for in

                  System.out.println(each);
               }

        }
    }
        
    复制代码

    (2)二维数组:

    声明方式:

    复制代码
    int[][] a = new int[2][2];
    int[][] b = new int[2][];
    int[][] c = {
        {1,2,3},
        {4,5},
        {7,8,9}
    };
    复制代码

    遍历数组:

    复制代码
    class Test 
    {      
            int[][] a = {
                {5,8,11},
                {1,23},
                {8,5,3}
            };
        public static void main(String[] args) 
        {
                    for(int i = 0;i<a.length;i++){
                for(int j = 0;j<a[i].length;j++){
                    System.out.println(a[i][j]);
                }
            }
        }
    }           
    复制代码

    求数组中数字的最大值:

    复制代码
    class Test 
    {    
        public static void main(String[] args) 
        {     
                    int[] a = {1,2,3,4};
            int max = a[0];
                    for(int i = 1;i<a.length;i++){
                if(a[i]>max){
                    max = a[i];
                }
            }
            System.out.println("最大值:"+max);
        }
    }
    复制代码

    (3)数组异常:

    复制代码
    class Test 
    {    
            int[][] a = {
            {1,2,3},
            {4,5},
            {7,8,9}
        };
            int[] b = {1,2,3};
        public static void main(String[] args) 
        {
            System.out.println(f[1][2]); //数组下标越界异常
            b = null;
            System.out.println(b[0]); //空指针异常
        }
    }
    复制代码

     2.传参:

    (1)数值传参

    复制代码
    class  Test
    {    
        public static int Change(int c,int b){
            c=0;  //数值传递
            b=0;
        }
        public static void main(String[] args) 
        {
            int a = 2;
            int b = 3;
            System.out.println(a+","+b);
            Change(a,b);
            System.out.println(a+","+b);
            //数值未改变,因为a,b只是作为形参被传入方法内,
           //c=0,b=0只是把传进去的参数覆写为0而已,原始数据不会改变
        }
    }
            
    复制代码

    (2)引用传参

    复制代码
    class  Test
    {       public static int Change(int[] a){
            a[0]=0;  //引用传递
        }
        public static void main(String[] args) 
        {
            int[] t1={1,2,3};
            System.out.println(t1[0]+","+t1[1]);
            Change(t1);
            System.out.println(t1[0]+","+t1[1]);
            //数值改变,因为t1这个数组被当做参数传入了方法中,所以方法中改变的就是t1这个数组本身的数据。
        }
    }
        
    复制代码

    3.方法重载

    方法名重复,形参的数据类型不同、个数不同以及传入顺序不同(三个条件满足一个即可视为方法重载),

    另外方法重载和返回值有无、返回值数据类型无关。

    JAVA会根据传入的形参的数据类型、个数以及传入顺序来选择调用的方法。

    复制代码
    public static void test(int a,intb){
         System.out.print(a+b);
    }
    public static void test(double a){
         System.out.print(a);
    }
    public static void test(double a,int b){
         System.out.print(a+b);
    }
    public static void test(int a,double b){
         System.out.print(a+b);
    }
    复制代码
  • 相关阅读:
    __del__ 析构方法 __init__ 构造方法
    单态模式
    面向对象小练习2
    __new__ '''魔术方法
    菱形继承
    继承:多继承
    继承: .单继承
    面向对象小练习
    __init__ 魔术方法
    如何访问私有成员
  • 原文地址:https://www.cnblogs.com/wangnatian/p/8681868.html
Copyright © 2011-2022 走看看