對于基本資料類型的變量建立的數組: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]);
}
}
}
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsIyZuBnL4IDM1YTOykzNx0CMxgTOxkTNyIDMxITM4EDMy0CO0UDM1UTMvwlMxgTMwIzLchDN1ATN1EzLcd2bsJ2Lc12bj5ycn9Gbi52YugTMwIzZtl2Lc9CX6MHc0RHaiojIsJye.png)
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]);
}
}
}
轉載于:https://www.cnblogs.com/KeepCalmAndNeverSayNever/p/10099805.html