您的位置 首页 java

java中给数组赋值的方法

1、数组操作中,可以使用等于(=)赋值

注意:此时新数组只是指向原数组的存储空间,并没有重新申请新的空间。

实例:

public class ArrayTest{public static void main(String args[]){// 1int[] a=new int[4];a[0]=1;a[1]=2;a[2]=3;a[3]=4;System.out.println(a[3]);// 2int b[]=new int[4];b[0]=1;b[1]=2;b[2]=3;b[3]=4;System.out.println(b[2]);// 3int[] c={1,2,3,4};int[] d=new int[]{1,2,3,4};System.out.println(c[2]);System.out.println(d[3]);}}

2、使用System.ararycopy方法

System.arraycopy(originalArray, 0, targetArray, 0, originalArray.length);

注意:新数组重新申请存储地址空间,再将原数组中数据拷贝过来。

推荐教程:Java教程

文章来源:智云一二三科技

文章标题:java中给数组赋值的方法

文章地址:https://www.zhihuclub.com/21801.shtml

关于作者: 智云科技

热门文章

网站地图