Java數組的三種初始化方法


import org.junit.Test;
//import org.testng.annotations.Parameters;

public class Demo {

    @Test
   public void test() {


//數組的靜態初始化
        int a[] = {2, 0, 1, 9, 2020};
       System.out.println("----------靜態初始化----------");
       getArray(a);
       
//數組的動態初始化方法一
        int[] b = new int[10];
        for (int i = 0; i < b.length; i++) {
            b[i] = i;
        }
       System.out.println("----------動態初始化1----------");
       getArray(b);

//數組的動態初始化方法二
        int[] c = new int[4];
        c[1] = 2;
        c[2] = 0;
        c[3] = 9;
       System.out.println("----------動態初始化2----------");
       getArray(c);

//數組的默認初始化,默認初始化只指定數組的長度,數組里面每個元素的值都是默認值
        int[]d=new int[5];
       System.out.println("----------默認初始化----------");
       getArray(d);

    }

    public void getArray(int[]arr){

        for (int j:arr){
            System.out.println(j);
            }
        }
}

 


免責聲明!

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



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