数组的复制
?數組復制有以下幾種方法:
示例代碼
package array;import java.util.Arrays;public class arrayCopy {/*** @jeasion Array Copy */public static void main(String[] args) {int[] a= new int[10];int[] b= new int[5];for(int i=0;i<10;i++){a[i]=(int)(Math.random()*10+1);}for(int j:a){System.out.print("\t@a:"+j);}System.out.println("\n賦值");//@1 數組賦值for(int i=3;i<8;i++){b[i-3]=a[i];}for(int j:b){System.out.print("\t@1:"+j);}System.out.println("\nclone()");//@2 數組克隆 array.clone()b=a.clone();for(int j:b){System.out.print("\t@2:"+j);}System.out.println("\nSystem.arraycopy()");//@3 System.arraycopy(source,first,aim,index,length);System.arraycopy(a,3,b,0,5);for(int j:b){System.out.print("\t@3:"+j);}System.out.println("\nArraycopyOf()");//@4 Arrays.copyOf(source,length) --Of 為大寫 length是新數組的長度,可以比原數組長//Arrays。copyOf(source,source。length+mount) 可以實現原數組長度的擴充b=Arrays.copyOf(a, 5);for(int j:b){System.out.print("\t@4:"+j);}System.out.println("\nArray.copyRanger()");//@5 Arrays.copyRange();b=Arrays.copyOfRange(a, 3, 7);for(int j:b){System.out.print("\t@5:"+j);}}}示例結果
@a:1 @a:10 @a:8 @a:3 @a:2 @a:8 @a:10 @a:8 @a:3 @a:2 賦值@1:3 @1:2 @1:8 @1:10 @1:8 clone()@2:1 @2:10 @2:8 @2:3 @2:2 @2:8 @2:10 @2:8 @2:3 @2:2 System.arraycopy()@3:3 @3:2 @3:8 @3:10 @3:8 @3:8 @3:10 @3:8 @3:3 @3:2 ArraycopyOf()@4:1 @4:10 @4:8 @4:3 @4:2 Array.copyRanger()@5:3 @5:2 @5:8 @5:10?
轉載于:https://www.cnblogs.com/jeasion/p/10758355.html
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
- 上一篇: 电脑回收站删除的文件怎么恢复,原来这么简
- 下一篇: jwt再度理解