Home Tutorial Java Corejava Array Relocation Example

 
 

Array Relocation Example
Posted on: April 15, 2010 at 12:00 AM
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:

Related Tags for Array Relocation Example:


Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.