數組元素的默認初始化值


對於基本數據類型的變量創建的數組:byte,short,int,long,float,double,char,boolean:
1.對於byte,short,long,int而言:創建數組以后默認值為0

package com;

public class V {
    public static void main(String[] args){
        int[] a=new int[3];//定義從0開始的三個數字的數組
        a[0]=70;
        a[2]=90;
        for(int i=0;i<a.length;i++){
            System.out.println(a[i]);
        }
    }
}


2.對於double,float而言:默認值為0.0

public class V {
    public static void main(String[] args){
        float[] a=new float[3];//定義從0開始的三個數字的數組
        a[0]=70F;
        a[2]=90F;
        for(int i=0;i<a.length;i++){
            System.out.println(a[i]);
        }
    }
}


3.對於char類型而言:默認值為空

public class V {
    public static void main(String[] args){
        char[] a=new char[3];//定義從0開始的三個數組
        a[0]='男';
        a[2]='女';
        for(int i=0;i<a.length;i++){
            System.out.println(a[i]);
        }
    }
}


4.對於Boolean而言:默認值為false

public class V {
    public static void main(String[] args){
        boolean[] a=new boolean[3];//定義從0開始的三個數組
        a[0]=true;
        a[2]=false;
        for(int i=0;i<a.length;i++){
            System.out.println(a[i]);
        }
    }
}

5.對於引用類型的變量構成的數組而言:默認初始化值為null

public class V {
    public static void main(String[] args){
        String[] a=new String[3];//定義從0開始的三個數組
        a[0]="陽陽";
        a[2]="寅寅";
        for(int i=0;i<a.length;i++){
            System.out.println(a[i]);
        }
    }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM