tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Java > Core > Arrays > Arrays Copy Range

Arrays Copy Range 

The following example shows how to use Arrays.copyOfRange API. It copies the specified range of the specified array into a new array. The initial index of the range (from) must lie between zero and original.length, inclusive and end index (to) is the final index of the range to be copied, exclusive.

File Name  :  
com/bethecoder/tutorials/utils/arrays/ArraysCopyRange.java 
Author  :  Sudhakar KV
Email  :  [email protected]
   
package com.bethecoder.tutorials.utils.arrays;

import java.util.Arrays;

public class ArraysCopyRange {

  public static void main (String [] args) {
    
    char [] chrArray = 'b''e''t''c''o''d''e''r' };
    System.out.println("Before copy : " + Arrays.toString(chrArray));
    
    //Copy array from index 3 to 8 (exclusive)
    char [] chrArray2 = Arrays.copyOfRange(chrArray, 38);
    chrArray2[4'd';
    
    System.out.println("Copied array : " + Arrays.toString(chrArray2));
    System.out.println("Original array : " + Arrays.toString(chrArray));
    
  }
}
   

It gives the following output,
Before copy : [b, e, t, c, o, d, e, r]
Copied array : [c, o, d, e, d]
Original array : [b, e, t, c, o, d, e, r]



 
  


  
bl  br