Java数组

JAVA学习网 2020-09-25 22:34:01

声明一个变量就是在内存空间划出一块合适的空间。声明一个数组就是在内存空间划出一串连续的空间。

数组基本要素:

  标识符:数组的名称,用于区分不同的数组

  数组元素:向数组中存放的数据

  元素下标:对数组元素进行编号,从0开始,数组中的每个元素都可以通过下标来访问

  元素类型:数组元素的数据类型

语法:数据类型    数组名[ ]   或  数据类型[ ]  数组名 ;

                //声明数组
		int [] score ;
		int score2[];
		//分配空间
		score= new int[3];
		score2= new int[6];
		//赋值
		score[2] = 48;
		score[0] = 90;
		score[1] = 67;
		//边声明边赋值
		int[] score3 = {55,78,90};
		int[] score4 = new int[] {88,33,77};		
		//边声明边分配
		int[] score5 = new int[3];				
		System.out.println((score[0]+score[1]+score[2])/3);
		
		/*
		 * 数组应用
		 */
		double [] list = {89.5,57,98,43.5};
		double max = list[0];
		double total = 0;
		for(int i =0;i<list.length;i++) {
			//打印所有数组元素
			System.out.print(list[i]+"\t");
		}
		for(int i=0;i<list.length;i++) {
			total = list[i]+total;			
		}
		System.out.println("所有元素之和是:"+total);
		for(int i =0;i<list.length;i++) {
			if(list[i]>max) {
				max = list[i];
			}
		}
		System.out.println("最大元素是:"+max);

  

阅读(2883) 评论(0)