Java knowledge points summary array

1. Overview of arrays
1. Understanding of arrays: Array is a collection of multiple data of the same type arranged in a certain order, named with a name, and managed uniformly through numbering.

2. Array-related concepts:

Array name
element name
subscript, subscript, index

The length of the array: the number of elements

3. Characteristics of arrays:

Arrays are sorted

Arrays are variables of reference data type. The elements of the array can be either basic data types or reference data types (the array acts as an element)

Creating an array object will open up a whole continuous space in memory

Once the length of the array is determined, it cannot be changed.

4. Classification of arrays:

According to the dimension: one-dimensional array, two-dimensional array,. . .

According to the array element type: array of basic data type elements, array of reference data type elements

5. Use of one-dimensional array
① Declaration and initialization of one-dimensional array

public class ArrayTest{
    
    
	public static void main(String[] args){
    
    
		int num;//声明
		num=10;//初始化
		int id=100;//声明+初始化

		int[] ids;//声明
		//静态初始化:数组的初始化和数组元素的赋值操作同时进行
		ids=new int[]{
    
    100,200,300,400,500};
		//动态初始化:数组的初始化和数组元素的赋值分开进行
		String[] names=new String[5];
		names[0]="逮虾户";
	}	
}

②How to call the element at the specified position of the array: call it through the subscript

//数组的角标(或索引)从0开始,到数组的长度-1结束。
String[] names=new String[5];
names[0]="张三";
names[1]="张三";
names[2]="张三";
names[3]="张三";
names[4]="张三";
//names[5]="张三",因为角标到数组长度-1,所以会报错

③How to get the length of the array

public class ArrayTest{
    
    
	public static void main(String[] args){
    
    
		int[] ids;//声明
		//静态初始化:数组的初始化和数组元素的赋值操作同时进行
		ids=new int[]{
    
    100,200,300,400,500};
		//动态初始化:数组的初始化和数组元素的赋值分开进行
		String[] names=new String[5];
		//length:属性
		System.out.println(names.length);
		System.out.println(ids.length);
		//数组的length和字符串的length()有些区别
		//length是属性不用加括号,而字符串的length()是方法
	}	
}

④How to traverse the array

public class ArrayTest{
    
    
	public static void main(String[] args){
    
    
		int[] ids;//声明
		//静态初始化:数组的初始化和数组元素的赋值操作同时进行
		ids=new int[]{
    
    100,200,300,400,500};
		//动态初始化:数组的初始化和数组元素的赋值分开进行
		for(int i=0;i<ids.length;i++){
    
    
		System.out.println(ids[i])
		}
	}	
}

⑤Default initialization value of array elements

Array elements are of integer type: 0
Array elements are of floating point type: 0.0
Array elements are of char type: 0 or '\u0000', not '0'
Array elements are of boolean type: false

Array elements are reference data types: null
reference data types: string or array act as array elements

6. Use of two-dimensional arrays:

规定:二维数组分为外层数组的元素,内层数组的元素
	int[][] arr=new int[4][3];
	外层元素:arr[0],arr[1]等
	内层元素:arr[0][0],arr[1][2]

①Declaration and initialization of two-dimensional array

public class ArrayTest2{
    
    
	public static void main(String[] args){
    
    
		int[] arr=new int[]{
    
    1,2,3};//一维数组
		int[] ar={
    
    1,2,3,4,5,6};
		//静态初始化
		int[][] arr1=new int[][]{
    
    {
    
    1,2,3},{
    
    4,5},{
    
    6,7,8,9}};
		//动态初始化1
		String[][] arr2=new String[3][2];
		//动态初始化2
		String[][] arr2=new String[3][];
		//其他写法
		int[] arr4[]=new int[][]{
    
    {
    
    1,2,3},{
    
    4,5,6,7},{
    
    8,9}}
		int[] arr5[]={
    
    {
    
    1,2,3},{
    
    4,5,6,7},{
    
    8,9}}
	}	
}

②.How to call the element at the specified position of the array

System.out.println(arr1[0][0]);//1
System.out.println(arr2[1][1]);//null

③.Get the length of the array

System.out.println(arr4.length);//3
System.out.println(arr4[0].length);//3
System.out.println(arr4[1].length);//4

④.How to traverse a two-dimensional array

for(int i=0;i<arr4.length;i++){
    
    
	for(int j=0;j<arr4[0].length;j++){
    
    
		System.out.print(arr4[i][j]+" ");
	}
}

⑤Default initialization value of array elements:

针对于初始化方式一:比如:int[][] arr=new int[4][3];
	外层元素的初始值为:地址值
	内层元素的初始值为:与一维数组初始化情况相同

针对于初始化方式二:比如:int[][] arr = new int[4][];
	外层元素的初始值为:null
	内层元素的初始值为:不能调用,否则报错。

Guess you like

Origin blog.csdn.net/m0_50760467/article/details/111185457