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[] { 1, 3, 5, 7, 9 };
showArray(number1);
int[] number2 = Arrays.copyOf(number1, 4);
number2[2] = 11;
showArray(number2);
int[] number3 = Arrays.copyOfRange(number1, 3, 10);
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: