更新時間:2022-07-18 13:19:45 來源:動力節點 瀏覽1453次
Java對象數組初始化是什么?動力節點小編給大家舉例說明。
public static void main(String[] args) {
String[] str = new String[10];//1 初始化數組
for (int i = 0; i < 10; i++){
System.out.println(str[i]);//打印出來的是null
}
for (int i = 0; i < 10; i++){
str[i] = new String("Hello");//2 為對象初始化
}
for (int i = 0; i < 10; i++){
System.out.println(str[i]);//打印出來的是Hello
}
}
它是創建了是個String類型的變量:str[0], str[1],….str[9]
但是沒有為它們創建對象,所以為它們的初始值都為null
str[i] = new String(“Hello”);創建了string對象,并讓
變量str[i]引用這個對象
對象數組初始化的第二種方法
String[] s = new String[]{"He", "me", "we", "sa", "she"};
相當于:
String[] s= new String[5];
s[0] = "He";
s[1] = "me";
s[2] = "we";
s[3] = "sa";
s[4] = "she";
以上就是關于“Java對象數組初始化”的介紹,大家如果想了解更多相關知識,可以關注一下動力節點的Java基礎教程,里面有更豐富的知識等著大家去學習,希望對大家能夠有所幫助。
0基礎 0學費 15天面授
有基礎 直達就業
業余時間 高薪轉行
工作1~3年,加薪神器
工作3~5年,晉升架構
提交申請后,顧問老師會電話與您溝通安排學習