Example usage for java.util Arrays copyOf

List of usage examples for java.util Arrays copyOf

Introduction

In this page you can find the example usage for java.util Arrays copyOf.

Prototype

public static boolean[] copyOf(boolean[] original, int newLength) 

Source Link

Document

Copies the specified array, truncating or padding with false (if necessary) so the copy has the specified length.

Usage

From source file:MainClass.java

public static void main(String args[]) {
    int arrayOriginal[] = { 42, 55, 21 };
    int arrayNew[] = Arrays.copyOf(arrayOriginal, 3);
    printIntArray(arrayNew);/*from w  ww . j a  va 2s.c  o  m*/
}

From source file:ArrayCopy.java

public static void main(String args[]) {
    System.out.printf("Before (original)\t%s%n", Arrays.toString(args));
    String copy[] = Arrays.copyOf(args, 4);
    System.out.printf("Before (copy)\t\t%s%n", Arrays.toString(copy));
    copy[0] = "A";
    copy[1] = "B";
    copy[2] = "C";
    copy[3] = "D";
    System.out.printf("After (original)\t%s%n", Arrays.toString(args));
    System.out.printf("After (copy)\t\t%s%n", Arrays.toString(copy));
}

From source file:ArrayReallocationDemo.java

public static void main(String[] args) {
    int[] data1 = new int[] { 1, 3, 5, 7, 9 };

    printArray(data1);/*from ww w  . j av  a 2  s  .  co  m*/
    int[] data2 = Arrays.copyOf(data1, 6);
    data2[5] = 11;
    printArray(data2);

    int[] data3 = Arrays.copyOfRange(data1, 2, 10);
    printArray(data3);
}

From source file:Main.java

public static void main(String[] args) {

    byte[] arr1 = new byte[] { 5, 15, 25 };

    System.out.println(Arrays.toString(arr1));

    // copying array arr1 to arr2 with newlength as 5
    byte[] arr2 = Arrays.copyOf(arr1, 5);
    arr2[3] = 0;//from   www .  j  a v a 2  s  .co  m
    arr2[4] = 1;

    System.out.println(Arrays.toString(arr2));
}

From source file:Main.java

public static void main(String[] args) {

    char[] arr1 = new char[] { 'j', 'a', 'v' };

    System.out.println(Arrays.toString(arr1));

    // copying array arr1 to arr2 with newlength as 5
    char[] arr2 = Arrays.copyOf(arr1, 5);
    arr2[3] = 'a';
    arr2[4] = '2';

    System.out.println(Arrays.toString(arr2));
}

From source file:Main.java

public static void main(String[] args) {
    boolean[] arr1 = new boolean[] { true, false };

    System.out.println(Arrays.toString(arr1));

    // copying array arr1 to arr2 with newlength as 4
    boolean[] arr2 = Arrays.copyOf(arr1, 4);

    // printing the array arr2
    System.out.println(Arrays.toString(arr2));
}

From source file:Main.java

public static void main(String[] args) {

    short[] arr1 = new short[] { 1, 10, 25 };

    System.out.println(Arrays.toString(arr1));

    // copying array arr1 to arr2 with newlength as 5 as Object
    Object arr2 = Arrays.copyOf(arr1, 5);

    // cast arr2 as short in order to be printable
    short[] arr3 = (short[]) arr2;

    System.out.println(Arrays.toString(arr3));
}

From source file:Main.java

public static void main(String[] args) {

    // intializing an array arr1
    int[] arr1 = new int[] { 1, 2, 3 };

    System.out.println(Arrays.toString(arr1));

    // copying array arr1 to arr2 with newlength as 5
    int[] arr2 = Arrays.copyOf(arr1, 5);
    arr2[3] = 4;/* ww  w.  j  a va2 s. c om*/
    arr2[4] = 5;

    System.out.println(Arrays.toString(arr2));
}

From source file:Main.java

public static void main(String[] args) {

    // intializing an array arr1
    short[] arr1 = new short[] { 1, 10, 15 };

    System.out.println(Arrays.toString(arr1));

    // copying array arr1 to arr2 with newlength as 5
    short[] arr2 = Arrays.copyOf(arr1, 5);
    arr2[3] = 100;//  www  . jav a2  s. c  om
    arr2[4] = 200;

    System.out.println(Arrays.toString(arr2));
}

From source file:Main.java

public static void main(String[] args) {

    // intializing an array arr1
    long[] arr1 = new long[] { 1L, 10L, 45L };

    System.out.println(Arrays.toString(arr1));

    // copying array arr1 to arr2 with newlength as 5
    long[] arr2 = Arrays.copyOf(arr1, 5);
    arr2[3] = 12L;/*from w  w w  . j  a  va  2 s .co m*/
    arr2[4] = 123456789L;

    System.out.println(Arrays.toString(arr2));
}