package day06;
public class ArrayTest2 {
public static void main(String[] args) {
//1.二维数组的声明和初始化
int [] arr =new int []{1,2,3}; //一维数组
int [][] arr1 =new int [][]{{1,2,3},{4,5},{6,7,8}};
//动态初始化1
String[][] arr2= new String [3][2];
//动态初始化2
String[][] arr3= new String [3][];
//错误情况:
//String[][] arr4= new String [][];
//String[1][3] arr5= new String [][];
//正确:
int [] arr7 [] =new int [][]{{1,2,3},{4,5},{6,7,8,9}};
int [] arr8 [] ={{1,2,3},{4,5},{6,7,8}};
//2.如何调用数组的指定位置的元素
System.out.println(arr1[0][1]);
System.out.println(arr2[1][1]);
arr3[1]=new String [4];
System.out.println(arr3[1][0]);
//3.如何获取数组的长度
System.out.println("arr7的长度为:"+arr7.length);//3
System.out.println("arr7[0]的长度为:"+arr7[0].length);//3
System.out.println("arr7[1]的长度为:"+arr7[1].length);//2
System.out.println("arr7[2]的长度为:"+arr7[2].length);//4
//4.如何遍历二维数组
for(int i=0;i<arr7.length;i++)
{
for(int j=0;j<arr7[i].length;j++){
System.out.print(arr7[i][j]+" ");
}
System.out.println();
}
//5.数组元素的默认初始化值
/* 规定:二维数组分为外层数组的元素,内层数组的元素
int[][]arr=new int [4][4];
外层元素: arr[0],arr[1]
内层元素:arr[0][0],arr[1][2];
*/
System.out.println("**********************");
int [][] arr9 =new int [4][3];
System.out.println(arr9[0]);//[I@15db9742,一个地址值
System.out.println(arr9[0][0]);//0
//System.out.println(arr9); [[I@6d06d69c
System.out.println("**********************");
//*****************************************
float [][] arr10 =new float [4][3];
System.out.println(arr10[0]);//[F@6d06d69c,一个地址值
System.out.println(arr10[0][0]);//0.0
System.out.println("**********************");
//*****************************************
String[][] arr11 =new String [4][2];
System.out.println(arr11[1]);//[Ljava.lang.String;@7852e922
System.out.println(arr11[1][1]);//null
System.out.println("**********************");
//*****************************************
double[][] arr12 =new double[4][];
System.out.println(arr12[1]);//null
// System.out.println(arr12[1][0]); 报错,空指针异常
}
}
package day06;
public class ArrayTest2 {
public static void main(String[] args) {
//1.二维数组的声明和初始化
int [] arr =new int []{1,2,3}; //一维数组
int [][] arr1 =new int [][]{{1,2,3},{4,5},{6,7,8}};
//动态初始化1
String[][] arr2= new String [3][2];
//动态初始化2
String[][] arr3= new String [3][];
//错误情况:
//String[][] arr4= new String [][];
//String[1][3] arr5= new String [][];
//正确:
int [] arr7 [] =new int [][]{{1,2,3},{4,5},{6,7,8,9}};
int [] arr8 [] ={{1,2,3},{4,5},{6,7,8}};
//2.如何调用数组的指定位置的元素
System.out.println(arr1[0][1]);
System.out.println(arr2[1][1]);
arr3[1]=new String [4];
System.out.println(arr3[1][0]);
//3.如何获取数组的长度
System.out.println(“arr7的长度为:”+arr7.length);//3
System.out.println(“arr7[0]的长度为:”+arr7[0].length);//3
System.out.println(“arr7[1]的长度为:”+arr7[1].length);//2
System.out.println(“arr7[2]的长度为:”+arr7[2].length);//4
//4.如何遍历二维数组
for(int i=0;i<arr7.length;i++)
{
for(int j=0;j<arr7[i].length;j++){
System.out.print(arr7[i][j]+” “);
}
System.out.println();
}
//5.数组元素的默认初始化值
/* 规定:二维数组分为外层数组的元素,内层数组的元素
int[][]arr=new int [4][4];
外层元素: arr[0],arr[1]
内层元素:arr[0][0],arr[1][2];
*/
System.out.println(“**“);
int [][] arr9 =new int [4][3];
System.out.println(arr9[0]);//[I@15db9742,一个地址值
System.out.println(arr9[0][0]);//0
//System.out.println(arr9); [[I@6d06d69c
System.out.println(“**“);
//*
float [][] arr10 =new float [4][3];
System.out.println(arr10[0]);//[F@6d06d69c,一个地址值
System.out.println(arr10[0][0]);//0.0
System.out.println(“**“);
//*
String[][] arr11 =new String [4][2];
System.out.println(arr11[1]);//[Ljava.lang.String;@7852e922
System.out.println(arr11[1][1]);//null
System.out.println(“**“);
//*
double[][] arr12 =new double[4][];
System.out.println(arr12[1]);//null
// System.out.println(arr12[1][0]); 报错,空指针异常
}
}