Array Relocation Example


 

Array Relocation Example

This is a very simple example of Array Relocation. After initialization we later relocate and assign value at any index.

This is a very simple example of Array Relocation. After initialization we later relocate and assign value at any index.

Description:

This example demonstrate the Array Relocation where value of any index can be change.

Code:

import java.util.Arrays;

public class ArrayReallocation {

  public static void main(String[] args) {

    int[] number1 = new int[] { 1357};
    showArray(number1);
    int[] number2 = Arrays.copyOf(number1, 4);
    number2[211;
    showArray(number2);
    int[] number3 = Arrays.copyOfRange(number1, 310);
    showArray(number3);
  }

  private static void showArray(int[] data) {
    StringBuffer stringBuffer = new StringBuffer("[");
    for (int i = 0; i < data.length; i++) {
      stringBuffer.append(data[i]);
      if (i < data.length - 1)
        stringBuffer.append(", ");
    }
    stringBuffer.append("]");
    System.out.println(stringBuffer);
  }
}

Output:

Ads