Java数组
文章目录
一、数组的概述
- 数组可以看成是多个相同类型数据集合,对这些数据统一管理。
- 数组变量属引用类型,数组也可以看成是对象,数组中的每个元素相当于该对象的成员变量。
- 数组中的元素可以是任何数据类型,基本数据类型和引用类型都行。
二、一维数组的声明
- 一维数组的声明方式:
type var[];//或者
type[] var;//或者
type []var;//这三种方式都可以
例如:
int a[];
int []b;
double c[];
String s1[];
Person[] p;//注意这是一个Person类型的数组,里面放的是Person对象的引用
注意:Java语言中声明数组时不能指定其长度(数组中元素的个数),例如:int a [5];//非法
三、数组对象的创建
Java中使用关键字new创建数组对象,格式为:
数组名 = new 数组元素类型[数组元素的个数]
四、元素为引用数据类型的数组
注意:元素为引用数据类型的数组中的每一个元素都需要实例化!具体看下面的例子:
五、数组初始化
- 动态初始化,数组定义与数组元素分配空间和赋值的操作分开进行,如:
/**
* Java 数组动态初始化
*/
public class ArrayDynamicInit {
public static void main(String[] args) {
int a[];
a = new int[3];
a[0] = 3;
a[1] = 4;
a[2] = 5;
Date date[];
date = new Date[3];
date[0] = new Date(2019,7,9);
date[1] = new Date(2019,7,10);
date[2] = new Date(2019,7,11);
}
static class Date{
int year,month,day;
Date(int year,int month,int day){
this.year = year;
this.month = month;
this.day = day;
}
}
}
- Java数组静态初始化,在定义数组的同时就为数组元素分配空间并赋值,示例代码如下:
/**
* Java数组静态初始化
*/
public class ArrayStaticInit {
public static void main(String[] args) {
int a[] = {1, 2, 3};
Date date[] = {new Date(2019, 7, 9),
new Date(2019, 7, 10),
new Date(2019, 7, 11)};
}
static class Date {
int year, month, day;
Date(int year, int month, int day) {
this.year = year;
this.month = month;
this.day = day;
}
}
}
六、数组元素的默认初始化
数组是引用类型,它的元素相当于类的成员变量,因此数组分配空间后,每个元素也被按照成员变量的规则被初始化。示例代码如下:
/**
* Java数组元素默认初始化
*/
public class InitArrayElem {
public static void main(String[] args) {
int a[] = new int[3];
System.out.println(a[0]);
Date date[] = new Date[3];
System.out.println(date[1]);
}
static class Date {
int year, month, day;
Date(int year, int month, int day) {
this.year = year;
this.month = month;
this.day = day;
}
}
}
//输出结果为:
七、数组元素的引用
八、二维数组的声明
二维数组必须先声明一维(左侧),否则是非法的。具体看下面代码:
public class TwoDimensionArrayInit {
//静态初始化
int array1[][] = {{1,2},{2,3,4},{7,3}};
int array2[3][2] = {{1,2},{2,3},{4,5}};//非法
//动态初始化
int array3[][] = new int[3][5];//声明时指定一维和二维所占空间
//
int array4[][] = new int[3][];
}
九、遍历二维数组
/**
* 遍历二维数组
*/
public class TwoDimensionArrayInit {
public static void main(String[] args) {
int array[][] = {{1, 2}, {3, 4, 5, 6}, {7, 8, 9}};
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
System.out.print(array[i][j] + " ");
}
}
}
}
输出结果:
十、数组的拷贝
使用java.lang.Sytem类的静态方法
public static void arrarcopy(Object src,int srcPos,Object dest,
int desPos,int length)
可以用于数组src从第srcPos项元素开始的length个元素拷贝到目标数组从desPos项开始的length个位置。如果源数据数目超过目标数组边界会抛出IndexOutOfBoundsException异常。
/**
* 拷贝一维数组
*/
public class TwoDimensionArrayInit {
public static void main(String[] args) {
String array[] = {"Alibaba", "Tencent", "Baidu", "Jingdong"};
String companyArray[] = new String[5];
System.arraycopy(array, 0, companyArray, 0, array.length);
for (int i = 0; i < companyArray.length; i++) {
System.out.print(companyArray[i] + " ");
}
}
}
输出结果:
由于原数组长度只有4,所有最后一个元素未赋值,默认为null,复制二维数组的代码如下:
/**
* 复制二维数组
*/
public class CopyTwoDimensionArray {
public static void main(String[] args) {
int array[][] = {{1, 2}, {1, 2, 3}, {3, 4}};
int arrayBak[][] = new int[3][];
System.arraycopy(array, 0, arrayBak, 0, array.length);
arrayBak[2][1] = 14;
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
System.out.print(array[i][j] + " ");
}
}
}
}
输出结果:
==由于arraybak复制了array,在内存中arraybak和array指向的是同一个引用,也就是说操作arraybak也就相当于操作了array,arraybak把4的值修改为14,array的值也就跟着发生了改变(通过画内存图一目了然,一般人我不告诉他,嘿嘿)。