1、数组的声明
数组是相同类型数据的有序集合。数组描述的是相同类型的若干个数据,按照一定的先后次序排列组合而成。其中,每一个数据称作一个元素,每个元素可以通过一个索引(下标)来访问他们。数组变量属于引用类型,数组也可以看成是对象,数组中的每个元素相当于该对象的成员变量。
int[] arr01 = new int [10]; //索引下标[0,length-1]:0,1,2,3,4,5,6,7,8,9
String arr02[] = new String[5];
User[] arr03 = new User[3];
数组的声明方式有两种(以一维数组为例):
1、type[] arr_name;//推荐使用这种方式
2、type arr_name[];
注意事项:
(1),声明的时候并没有实例化任何对象,只有在实例化数组对象时,JVM才分配空间,这时才与长度有关。
(2),声明一个数组的时候并没有数组真正被创建。
(3),构造一个数组,必须指定长度。
2、数组的三个基本特点:
(1)长度是确定的。数组一旦被创建,它的大小就是不可以改变的。
(2)其元素必须是相同类型,不允许出现混合类型。
(3)数组类型可以是任何数据类型,包括基本类型和引用类型。
创建一个基本类型的一维数组
public class Test {
public static void main(String args[]) {
int[] a = new int[10];
for(int i=0;i<10;i++) {
a[i]=2*i;
System.out.println(a[i]);
}
}
}
3、数组的初始化方式总共有三种:静态初始化、动态初始化、默认初始化。
(1)静态初始化:
除了用new关键字来产生数组以外,还可以直接在定义数组的同时就为数组元素分配空间并赋值。
int[] b = {1,2,3,4};
User[] c = {
new User(1000,"小红"),
new User(1001,"小明"),
new User(1002,"小亮")
};
(2)动态初始化:
数组定义与为数组元素分配空间并赋值的操作分开进行
int[] a1=new int[2];//动态初始化数组,先分配空间;
a1[0]=1;//给数组元素赋值
a1[1]=2;//给数组元素赋值
(3)默认初始化
数组是引用类型,它的元素相当于类的实例变量,因此数组一经分配空间,其中的每个元素也被按照实例变量同样的方式被隐式初始化。
int[] a2= new int[2];//默认值:0,0
boolean[] a3=new boolean[2];//默认值:false,false
String[] a4=new String[2];//默认值:null,null
4、数组的遍历
数组元素下标的合法区间:[0,length-1]。我们可以通过下标来遍历数组中的元素,遍历时可以读取元素的值或者修改元素的值。
使用循环遍历初始化和读取数组
public class Test {
public static void main(String args[]) {
int[] a = new int[4];
for(int i=0;i<a.length;i++) {
a[i]=2*i;
}
for(int i=0;i<a.length;i++) {
System.out.println(a[i]);
}
}
}
5、for-each循环
增强for循环,for-each是JDK1.5新增加的功能,专门用于读取数组或集合中所有的元素,即对数组进行遍历。
6、数组的拷贝
System类里也包含了一个static void arraycopy(object src,int srcpos,object dest,int destpos,int length)方法,该方法可以将src数组里的元素值赋给dest数组的元素,其中srcpos指定从src数组的第几个元素开始赋值,length参数指定将src数组的多少个元素赋给dest数组的元素。
public class testarraycopy {
public static void main(String[] args) {
int[] a1= {1,2,3,4,5};
int[] a2= new int[10];
System.arraycopy(a1, 2, a2, 6, 3);
for(int i=0;i<a2.length;i++) {
System.out.print(a2[i]+" ");
}
}
}
0 0 0 0 0 0 3 4 5 0
测试数组中指定索引位置的元素,并将原数组返回
public class testarraycopy {
public static void main(String[] args) {
String[] str= {"小红","小明","小强","小亮","小壮"};
removeElement(str,3);
}
public static String[] removeElement(String[] s,int index) {
System.arraycopy(s, index+1, s, index, s.length-index-1);
s[s.length-1]=null;
for(int i=0;i<s.length;i++) {
System.out.println(i+"--"+s[i]);
}
return s;
}
}
0--小红
1--小明
2--小强
3--小壮
4--null
测试从数组中删除某个元素(本质上还是数组的拷贝)
public class testarraycopy {
public static void main(String[] args) {
testDelete();
}
public static void testDelete() {
String[] s1= {"a","b","c","d","e"};
System.arraycopy(s1, 3, s1, 3-1, s1.length-3);
s1[s1.length-1]=null;
for(int i=0;i<s1.length-1;i++) {
System.out.print(i+"--"+s1[i]+" ");
}
}
}
0--a 1--b 2--d 3--e
数组的扩容(本质上是:先定义一个更大的数组,然后将原数组内容原封不动拷贝到新数组中)
public class testarraycopy {
public static void main(String[] args) {
extend();
}
public static void extend() {
int[] a1= {1,2,3,4,5};
int[] a2=new int[a1.length+10];
System.arraycopy(a1, 0, a2, 0, a1.length);
for(int m:a2) {
System.out.print(m+" ");
}
}
}
1 2 3 4 5 0 0 0 0 0 0 0 0 0 0
7、多维数组
多维数组可以看成是以数组为元素的数组,可以有二维,三维,甚至更多维数组,但是实际开发应用中用的非常少,最多到二维数组。
(1)二维数组的声明
public class Test {
public static void main(String args[]) {
int[][] a= new int[3][];
a[0]=new int[] {20,30};
a[1]=new int[] {10,15,40};
a[2]=new int[] {50,90};
System.out.println(a[1][2]);
}
}
40
(2)二维数组的静态初始化
public class Test {
public static void main(String args[]) {
int[][] b= {
{10,20,30},
{25,35},
{68,78,99,80}
};
System.out.print(b[2][3]+" ");
System.out.println(b[1][1]);
}
}
80 35
今天先这样,明天再继续奋斗✊