数组的扩容方法、数组的传参、可变长参数
- 6.2 数组的操作
- 6.2.1 数组的扩容
- 6.2.2 复制的方法
- 6.2.3 地址的替换
- 6.2.4 数组类型的参数
- 6.2.5 数组类型的返回值
- 6.2.6 可变长参数
6.2 数组的操作
6.2.1 数组的扩容
- 创建数组时,必须显示指定长度,并再创建之后不可更改长度;
- 扩容的思路:
创建大于原数组长度的新数组;
将原数组中的元素依次复制到新数组中;
6.2.2 复制的方法
public class TestCopyArray{
public static void main(String[] args){
int[] oldArray = new int[]{1,2,3,4,5};
int[] newArray = new int[oldArray.length * 2];
for(int i = 0;i < oldArray.length;i++){
newArray[i] = oldArray[i];
}
for(int i = 0;i < oldArray.length;i++){
System.out.print(oldArray[i] +" ");
}
System.out.println();
for(int i = 0;i < newArray.length;i++){
System.out.print(newArray[i] +" ");
}
}
}
- System.arraycopy(原数组 , 原数组起始 , 新数组 , 新数组起始 , 长度);
public class TestCopyArray2{
public static void main(String[] args){
int[] oldArray = new int[]{1,2,3,4,5};
int[] newArray = new int[oldArray.length * 2];
System.arraycopy(oldArray,0,newArray,0, oldArray.length);
for(int i = 0;i < oldArray.length;i++){
System.out.print(oldArray[i] +" ");
}
System.out.println();
for(int i = 0;i < newArray.length;i++){
System.out.print(newArray[i] +" ");
}
}
- java.util.Array.copyOf(原数组 , 新长度); //返回带有原值的新数组
public class TestCopyArray3{
public static void main(String[] args){
int[] oldArray = new int[]{1,2,3,4,5};
int[] newArray = new int[oldArray.length * 2];
newArray = java.util.Arrays.copyOf(oldArray , oldArray.length * 2);
for(int i = 0;i < oldArray.length;i++){
System.out.print(oldArray[i] +" ");
}
System.out.println();
for(int i = 0;i < newArray.length;i++){
System.out.print(newArray[i] +" ");
}
}
}
6.2.3 地址的替换
- 数组作为引用类型之一,其变量中存储的是数组的地址;
- 完成元素复制后,需将新数组地址,赋值给原变量进行替换;
public class TestAddress{
public static void main(String[] args){
int a = 10;
String str = "abc";
int[] nums;
nums = new int[4];
nums[0] = 10;
System.out.println(nums);
double[] scores = new double[4];
System.out.println(scores);
int[] newNums = new int[5];
System.out.println(nums.length);
nums = newNums;
System.out.println(nums.length);
}
}
6.2.4 数组类型的参数
public class TestParams{
public static void main(String[] args){
int[] nums = new int[]{1,2,3,4,5};
int[] arr = new int[]{4,5,6,7,8};
print(nums);
print(arr);
}
public static void print(int[] array){
for(int i = 0;i < array.length;i++){
System.out.print(array[i] +"\t");
}
System.out.println();
}
}
- 方法调用时,将nums中的地址赋值给array,此时二者指向同一个数组;
6.2.5 数组类型的返回值
public class TestReturnValue{
public static void main(String[] args){
int[] nums = {1,2,3,4,5};
int[] newNums = expand(nums);
for(int i = 0;i < newNums.length;i++){
System.out.print(newNums[i] +"\t");
}
System.out.println();
}
public static int[] expand(int[] array){
int[] newArray = new int[array.length * 2];
for(int i = 0;i < array.length;i++){
newArray[i] = array[i];
}
return newArray;
}
}
- 创建新数组,长度为原数组的2倍,新数组中保留原有数据,返回新数组;
6.2.6 可变长参数
- 概念:
可接收多个同类型实参,个数不限,使用方法与数组相同;
- 语法:
数据类型... 形参名
public class TestChangeLength{
public static void main(String[] args){
int[] numbers= {1,2,3,4,5};
method(numbers);
method(5,6,7,8,9);
int[] newArray = expand(11,22,33,44,55,66);
method(newArray);
}
public static void method(int... arr){
for(int i = 0;i < arr.length;i++){
System.out.print(arr[i] +"\t");
}
System.out.println();
}
public static int[] expand(int length , int... arr){
int[] newNums = new int[length];
System.arraycopy(arr , 0 , newNums , 0 , arr.length);
return newNums;
}
}