代码
import java.util.Arrays;
class Untitled {
public static void main(String[] args) {
//新建一个数组
//int[] arr1 = new int[]{1,2,3,4};
int[] arr1 = {1,2,3,4};
System.out.println(Arrays.toString(arr1));
//获取数组长度
int length = arr1.length;
System.out.println("arr1的长度" + length);
//访问数组元素
System.out.println("arr1的第三个元素是"+arr1[2]);
//为数组中的元素赋值
arr1[2] = 6;
System.out.println("arr1的第三个元素是"+arr1[2]);
for(int i =0;i<arr1.length;i++)
{
int j = i+1;
System.out.println("第"+j+"个元素是:"+arr1[i]);
}
//新数组替换旧数组增加一个元素
int[] arr2 = new int[arr1.length+1];
for(int i=0;i<arr1.length;i++)
{
arr2[i] = arr1[i];
}
System.out.println("复制完arr1元素的arr2"+Arrays.toString(arr2));
arr2[arr2.length-1] = 5;
System.out.println("增加数据后的arr2"+Arrays.toString(arr2));
arr1 = arr2;
System.out.println("替换后的arr1" + Arrays.toString(arr1));
//删除一个元素同理 1.创建一个新数组 2.复制元素 3,添加或删除元素 4.替换数组
}
}
结果