Java System.arraycopy方法の使用

1765 ワード

无言ではなく、コードを直接见れば分かります。中//後は運行の結果です。
import java.util.Arrays;
/**
 *    JAVA     - System.arraycopy     。<br>
 * <br>
 *              ,          ,<br>
 *             
 *
 * @author      (java2000.net,laozizhu.com)
 *
 */
public class LessionSystemArraycopy {
  public static void main(String[] args) {
    //     native  。
    // public static native void arraycopy(
    // Object src, int srcPos, Object dest,
    // int destPos, int length);
    //    
    int[] ids = { 1, 2, 3, 4, 5 };
    System.out.println(Arrays.toString(ids)); // [1, 2, 3, 4, 5]
    //       
    //     0   2         3    
    System.arraycopy(ids, 0, ids, 3, 2);
    System.out.println(Arrays.toString(ids)); // [1, 2, 3, 1, 2]
    //           
    //       1   3            0    
    int[] ids2 = new int[6];
    System.arraycopy(ids, 1, ids2, 0, 3);
    System.out.println(Arrays.toString(ids2)); // [2, 3, 1, 0, 0, 0]
    //      
    //       +        
    //       +        
    //            
    try {
      System.arraycopy(ids, 0, ids2, 0, ids.length + 1);
    } catch (IndexOutOfBoundsException ex) {
      //       ,      
      System.out.println("      :    。");
    }
    System.out.println(Arrays.toString(ids2)); // [2, 3, 1, 0, 0, 0]
    //          
    Object[] o1 = { 1, 2, 3, 4.5, 6.7 };
    Integer[] o2 = new Integer[5];
    System.out.println(Arrays.toString(o2)); // [null, null, null, null, null]
    try {
      System.arraycopy(o1, 0, o2, 0, o1.length);
    } catch (ArrayStoreException ex) {
      //       ,             
      System.out.println("      :      ,    。");
    }
    //     ,  3              。      
    System.out.println(Arrays.toString(o2)); // [1, 2, 3, null, null]
  }
}