zoukankan      html  css  js  c++  java
  • 数组的复制

     数组复制有以下几种方法:

    1. 通过for循环进行数值复制
    2. 源数组名.clone()
    3. public static native void arraycopy(Object src,  int  srcPos,Object dest, int destPo, int length);(System类方法)
    4.  public static int[] copyOf(int[] original, int newLength)   (Arrays类方法)
    5.  public static int[] copyOfRange(int[] original, int from, int to)  (Arrays类方法)

    示例代码

    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("	@a:"+j);
    		}
    		System.out.println("
    赋值");
    		//@1 数组赋值
    		for(int i=3;i<8;i++){
    			b[i-3]=a[i];
    			
    		}
    		for(int j:b){
    			System.out.print("	@1:"+j);
    		}
    		System.out.println("
    clone()");
    		
    		//@2 数组克隆 array.clone()
    		b=a.clone();
    		for(int j:b){
    			System.out.print("	@2:"+j);
    		}
    		System.out.println("
    System.arraycopy()");
    		
    		//@3 System.arraycopy(source,first,aim,index,length);
    		System.arraycopy(a,3,b,0,5);
    		for(int j:b){
    			System.out.print("	@3:"+j);
    		}
    		System.out.println("
    ArraycopyOf()");
    		
    	//@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("	@4:"+j);
    		}
    		System.out.println("
    Array.copyRanger()");
    		//@5 Arrays.copyRange();
    		b=Arrays.copyOfRange(a, 3, 7);
    		for(int j:b){
    			System.out.print("	@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
  • 相关阅读:
    10.22 比赛总结 修剪草坪(mowlawn)、玉米迷宫(cornmaze)
    QProcess怎么实时的读到output的信息
    Qt Visual Studio Add-in 导出的 .pri 怎么用?
    Qt写Activex插件 总结
    osg中内嵌QtBrowser
    解决使用osgModeling的Loft生成管子时的bug(续)
    lua中的中文乱码
    std::string的split函数
    解决使用osgModeling的Loft生成管子时的bug
    Qt里的slot
  • 原文地址:https://www.cnblogs.com/jeasion/p/10758355.html
Copyright © 2011-2022 走看看