加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

数组拷贝

发布时间:2020-12-15 07:23:14 所属栏目:Java 来源:网络整理
导读:1 import java.util.Arrays; 2 3 public class Test01 { 4 public static void main(String[] args) { 5 // 数组拷贝方式1 6 // int[] a= {1,2,3,4,5}; 7 // int[] b= new int[a.length]; 8 // for(int i=0;ia.length;i++) { 9 // b[i]=a[i]; 10 // } 11 //
 1 import java.util.Arrays;
 2 
 3 public class Test01 {
 4     public static void main(String[] args) {
 5 //        数组拷贝方式1
 6 //        int[] a= {1,2,3,4,5};
 7 //        int[] b= new int[a.length];
 8 //        for(int i=0;i<a.length;i++) {
 9 //            b[i]=a[i];
10 //        }
11 //        for(int i=a.length-1,j=a.length/2;i>=a.length/2;i--,j++) {
12 //            b[j]=a[i];
13 //        }
14 //        System.out.println(Arrays.toString(b));
15         
16         //----------------------------
17         //数组拷贝方式2,Arrays.copyOf(要拷贝的原始数组,要拷贝的元素个数)
18 //        int[] a=new int[] {1,-3,5};
19 //        int[] b=Arrays.copyOf(a,a.length);
20 //        System.out.println(Arrays.toString(b));
21 //        
22         //----------------------------
23         //数组拷贝方式3,Arrays.copyOfRange(要拷贝的原始数组,起始下标from,截止下标to)
24         //拷贝的元素个数=to-from或者拷贝的元素的范围为:[from,to),能取到from,取不到to
25 //        int[] a=new int[] {1,5};
26 //        int[] b=Arrays.copyOfRange(a,1);
27 //        System.out.println(Arrays.toString(b));
28         
29         //----------------------------
30         //数组拷贝方式4,System.arraycopy(要拷贝的原始数组,原始数组的起始下标from,目标数组brr,目标数组的其实下标from2,要拷贝的元素的个数)
31 //        int[] a= {1,5,6};
32 //        int[] b=new int[a.length];
33 //        System.arraycopy(a,1,b,3);
34 //        //拷贝的数组元素个数<=数组b的length
35 //        System.out.println(Arrays.toString(b));
36         
37         //数组拷贝方式5,a.clone()
38         int[] a= {1,5};
39         int[] b=a.clone();
40         System.out.println(Arrays.toString(b));
41         
42     }
43 }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读