Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

import java.lang.reflect.Array;
import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        int[] ids = new int[2];
        System.out.println(ids.length);
        System.out.println(Arrays.toString(ids));

        ids = (int[]) expandBy(ids, 2);

        ids[2] = 3;
        System.out.println(ids.length);
        System.out.println(Arrays.toString(ids));
    }

    public static Object expandBy(Object oldArray, int increment) {
        Object newArray = null;
        int oldLength = Array.getLength(oldArray);
        int newLength = oldLength + increment;
        Class<?> c = oldArray.getClass();
        newArray = Array.newInstance(c.getComponentType(), newLength);
        System.arraycopy(oldArray, 0, newArray, 0, oldLength);
        return newArray;
    }
}