java语言中的数组
数组是一组连续的存储空间,存储多个相同数据类型的值。
1、数组的创建、赋值与使用
public class Test{
public static void main(String[] args){
//声明int型数组,变量名为a,分配长度为5的连续空间
int[] a = new int[5];
//依次为数组赋值
a[0] = 1;
a[1] = 2;
a[2] = 3;
a[3] = 4;
a[4] = 5;
//依次输出数组
System.out.println(a[0]);
System.out.println(a[1]);
System.out.println(a[2]);
System.out.println(a[3]);
System.out.println(a[4]);
}
}
//输出结果:
// 1
// 2
// 3
// 4
// 5
创建数组的语法:
#创建数组的语法分为动态初始化和静态初始化:
动态初始化:先定义,后赋值(下面的1和2是动态);
静态初始化:定义的同时直接进行赋值(下面的3和4是静态)。
public class Test{
public static void main(String[] args){
//(1)先声明、再分配空间
// 数据类型[] 数组名;
// 数组名 = new 数据类型[长度];
int[] array1;
array1 = new int[4];
//(2)声明并分配空间
// 数据类型[] 数组名 = new 数据类型[长度];
int[] array2 = new int[4];
//(3)声明并赋值(繁)
// 数据类型[] 数组名 = new 数据类型[]{value1,value2,value3,...};
int[] array3 = new int[]{ 11 , 22 , 33};
//(4)声明并赋值(简)
// 数据类型[] 数组名 = {value1,value2,value3,...}; (注意:不可换行)
int[] array4 = { 66,77,88 };
}
}
2、数组的特点
(1)数组中的每个数据格被称为“数组元素”;
(2)数组存储的是相同类型的数据;
(3)对每个元素进行赋值或取值的操作被称为“元素的访问”;
(4)访问元素时,需要使用“下标”(从0开始,依次+1,自动生成,最大值为长度-1);
(5)创建数组时,必须指定长度,并在创建之后不可更改长度;
(6)访问的语法:数组名[下标]; //例如 存:a[0]=10; 取:a[0]。
注意:
有效下标范围:0 ~ 数组长度-1;
访问无效下标,会导致数组下标越界。
3、数组的遍历
遍历就是从头至尾,逐一对数组的每个元素进行访问。
public class Test{
public static void main(String[] args) {
int[] a = new int[5];
a[0] = 1;
a[1] = 2;
a[2] = 3;
a[3] = 4;
a[4] = 5;
//数组名.length可动态获得数组长度
for(int i = 0;i < a.length;i++){
//使用循环变量“i”充当下标,逐一访问数组中的每个元素
System.out.println(a[i]);
}
}
}
数组的默认值:
(1)int:0
(2)double:0.0
(3)char:\u0000
(4)boolean:false
(5)其他:null
public class Test{
public static void main(String[] args) {
int[] a = new int[5];
//在没有为数组元素赋值的情况下,依旧可以正确访问。
for(int i = 0;i < a.length;i++){
System.out.println(a[i]);
}
}
}
//输出结果:
// 0
// 0
// 0
// 0
// 0
#数组的遍历是利用循环输出数组的内容,而循环的方式有2种:
(1)遍历循环:for (循环变量类型 循环变量名称;循环条件;更新语句) 循环体
(2)增强型for循环:for (循环变量类型 循环变量名称 : 要被遍历的对象) 循环体
4、数组的扩容
扩容的思路:
- (1)创建大于原数组长度的新数组。
- (2)将原数组中的元素依次复制到新数组中。
复制的方式:
#方式1:循环将原数组中所有元素逐一赋值给新数组
public class Main{
public static void main(String[] args){
//数组创建之后,长度不可改变
int[] nums = {11,22,33,44,55};
//1.创建比原数组大的新数组
int[] newNums = new int[ nums.length * 2 ];
//2.复制原数组中的所有数据到新数组
for(int i = 0 ; i < nums.length ; i++){
newNums[i] = nums[i];
}
//3.把旧数组对象指向新数组对象
nums = newNums;
nums[5] = 66;
//4.遍历
for(int i = 0 ; i < nums.length ; i++){
System.out.print( nums[i] +" ");
}
}
}
#方式2:
System提供了一个静态方法arraycopy(),我们可以使用它来实现数组之间的复制;
使用格式:System.arraycopy(原数组,原数组起始,新数组,新数组起始,长度)
public class Main{
public static void main(String[] args){
//数组创建之后,长度不可变
int[] nums = {11,22,33,44,55};
//1.创建新数组
int[] newNums = new int[ nums.length * 2 ];
//2.使用System.arraycopy(原数组,原数组起始下标,新数组,新数组的起始存储下标,需要赋值的个数或长度);
System.arraycopy( nums , 0 , newNums , 0 , nums.length);
//3.遍历
for(int i = 0 ; i < newNums.length ; i++){
System.out.print( newNums[i] +" ");
}
}
}
#方式3:
Arrays.copyOf功能是实现数组的复制,返回复制后的数组;
使用格式:java.util.Arrays.copyOf(原数组, 新长度)
public class Main{
public static void main(String[] args){
//数组创建之后,长度不可变
int[] nums = {11,22,33,44,55};
//1.创建新数组、复制元素
int[] newNums = java.util.Arrays.copyOf(nums , nums.length * 2);
//2.遍历
for(int i = 0 ; i < newNums.length ; i++){
System.out.print( newNums[i] +" ");
}
}
}