JAVA-引用数据类型之一(数组)

一、数组

1.1、定义:

数组是相同数据类型的多个数据的容器

1.2、创建格式:


常用数组常见格式

格式1. 数据类型[] 数组名称 = {数组内容 1,数组内容 2,数组内容 3…数组内容 n};

public static void main(String[] args) {
    
    	
		// 格式1:数组类型[] 数组名称 = {数据1,数据2,......,数据n};
		// 创建数组的同时,并创建数组的内容
		int[] student_score1 = {
    
     90, 50, 80, 60, 70, 60 };		
	}

格式2. 数据类型[] 数组名称 = new 数据类型[数组长度];

public static void main(String[] args) {
    
    	
		// 格式2:数组类型[] 数组名称 = new 数组类型[数组长度]; 
		// 创建数组,并指定长度,不指定数组里面的内容	
		int[] student_score2 = new int[6];		
	}

不常用的数组创建格式

格式 3. 数据类型[] 数组名;

public static void main(String[] args) {
    
    	
		// 格式3:数组类型[] 数组名称; 
		// 创建数组不初始化
		int[] student_score3;
		student_score3 = new int[6];//但需要new来初始化		
	}

格式 4. 数据类型[] 数组名称 = new 数据类型[]{内容 1,内容 2,内容 3…内容 n};

public static void main(String[] args) {
    
    	
		// 格式4:数据类型[] 数组名称 = new 数据类型[]{数据1,数据2,......,数据n};
		int[] student_score4 = new int[] {
    
    80,70,88,66,99,100};		
	}

1.3、数组长度获取

数组名称.length

public static void main(String[] args) {
    
    	
		// 格式4:数据类型[] 数组名称 = new 数据类型[]{数据1,数据2,......,数据n};
		int[] student_score4 = new int[] {
    
    80,70,88,66,99,100};	
		System.out.println(student_score4 .length);
	}

结果:

6

总结:

关于数组的一些常用创建格式总结到此,如有疑问欢迎留言评论。


猜你喜欢

转载自blog.csdn.net/mjh1667002013/article/details/113409568