数组
数组:只能是同一类型,必须是连续的,必须是大小不可分的;
pubilc static void main (String [] args){
int[] array = new int[5];
int[]引申出 —— int a = 0;
int b = 0;
int c = 0;
.....
student[] allstus = new student[5];
数组:1:声明;2:使用new
长度-1,从0开始;
第一个:引用+元素长度*0;
第二个:引用+元素长度*1;
第三个:引用+元素长度*2;
第四个:引用+元素长度*3;
第n个:引用+元素长度*(n-1);
数组长度:长度不可变, 长度表示:.length
引用数组:值传递
数组复制:数组大小固定,创建一个新数组,然后用for循环挨个拷贝。
扩容:增加数组内容
int[] a = {1,3,5,7,9}
int[] b = new int[a.length+1]; // 增加扩容1
for(int i = 0; i<a.length; i++){
b[i] = a[i]; // 将a的数组赋值给b数组
}
a = b
System.out.println(a.length); //可打印a数组看是否扩容
数组并接:n个数组相加并接;
int[] a = {1,3,5};
int[] b = {7,9,0};
int[] c = new int [a.length+b.length];
for(int i = 0; i < a.length; i++){
c[i] = a[i];
}
for(int j = 0; j < b.length; j++){
c[i + a.length] = b[i];
}
假装有示意图
数组减下标:
int[] array = {5,3,-9,1,24};
Scanner scan =new Scanner(System.in);
System.out.println("请输入你要删除的元素下标:");
int index = scan.nextInt();
for(int i = index; i<array.length-1 ; i++){
array[index] = array[index=1];
}
array[array.length-1] = 0;
}
当数组作为形参时,有两种声明方式:
1、传统方式:[] ——— 调用者只能传递数组对象进来。
public static void changelase(char[] array){
2、在JDK1.5之后更新方式:... ———调用者既可以传递数组对象,也可以直街传递任意个数组的元素进来。
public static void changelase(char... array){