数组
作者:互联网
数组
数组概述
定义
- 数组是相同类型数据的有序集合
- 数组描述的事项同类型的若干个数据,按照一定的吸纳后次序排列组合而成
- 其中每个数据称作一个数组元素,每个元素可以通过一个下表来访问
数组的声明创建
-
首先必须声明数组变量,才能在程序中使用数组。
-
数组变量声明语法
dataType[] arrayRefVar;//首选 //或是 dataType arrayRefVar[];//效果相同,但不是首选方法
如:
int[]nums;//定义数组 nums=new int[10];//固定数组的初始大小,表示里面可以存放10个int类型的数字
-
Java语言使用new操作符来创建数组
-
语法如下:
dataType[] arrayRefVar = new dataType[arraySize];
如:
nums=new int[10];//表示里面可以存放10个int类型的数字
-
数组的元素是通过索引访问的,数组索引冲0开始
-
获取数组长度:
array.length
-
例子:
public static void main(String[] args) { int[]nums;//步骤1.定义数组 nums=new int[3];//步骤2.表示里面可以存3放个int类型的数字 //步骤3.给数组元素赋值 注意,数组是从0开始检索的 nums[0]=10; nums[1]=10; nums[2]=10; //计算所有元素的和 int sum = 0; //获取数组长度可用 array.length for (int i = 0; i < nums.length; i++) { sum+=nums[i]; } System.out.println("次数组元素和为:"+sum); }
标签:10,nums,int,dataType,数组,new 来源: https://www.cnblogs.com/piaoxuesanqian/p/15854935.html