东莞商城网站建设公司,dw怎样做网站链接,中建八局第一建设有限公司是国企吗,做瑷网站数组的的定义 数组是存放在连续存储空间的元素集合 数组定义的格式#xff1a; int[] arrnew int[5]; int#xff1a;数组元素的数据类型#xff0c;可以是基本数据类型#xff0c;也可以是引用 arr#xff1a;数组名称 5#xff1a;数组中元素个数 第一步#xff1a;定…数组的的定义 数组是存放在连续存储空间的元素集合 数组定义的格式 int[] arrnew int[5]; int数组元素的数据类型可以是基本数据类型也可以是引用 arr数组名称 5数组中元素个数 第一步定义数组类型和数组名称 int [ ] arr; 第二步在堆中产生数组并定义数据大小 arrnew int [10 ] 第三步使用数组并赋予元素值 arr [0] 33; 或者直接列举法给数组赋值int arr{125, 3, 56, 78} 注意列举法的数组数据产生在方法区中 数组的特点 1、元素的数据类型是固定的 2、数组一旦定义完成内存中存放元素的空间也就确定下来了 3、在定义的时候就需要确定数组大小而且一旦确定将无法再次修改 数组的扩容 1 给数组添加元素 public static void add(int element){ //创建add方法并传入参数element size ; //size是元素个数 if(size length) { int[] news new int[size]; System.arraycopy(arrs, 0, news, 0, length); arrs news; } arrs[size -1] element; //在数组最后加入元素element length arrs.length; } 2 根据下标修改元素 array[ i ]element; //修改元素直接赋值 3 根据下标获得元素 public static int get(int index) { if(index 0 index (length -1)) { return arrs[index]; // 返回数组值 为了保证数据精度返回类型必须和数组数据类型一致 }else { //超过0--(length-1)的范围都属于数组越界 throw new ArrayIndexOutOfBoundsException(index); } } 4 根据下标清空元素 public static void remove(int index) { if(index 0 index (length -1)) { size --; //size为数组元素个数 int[] news new int[length -1]; System.arraycopy(arrs, 0, news, 0, index); if(index ! (length -1)) { System.arraycopy(arrs, (index 1), news, index, (length - 1 -index)); } arrs news; length arrs.length; }else { //超过0--(length-1)的范围都属于数组越界 throw new ArrayIndexOutOfBoundsException(index); } } 转载于:https://www.cnblogs.com/lloney0/p/10992461.html