Home Tutorial Java Corejava Array Relocation Example

 
 

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

Advertisement

Related Tags for Array Relocation Example:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: April 15, 2010

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com