main.java

来自「java的经典例子」· Java 代码 · 共 27 行

JAVA
27
字号
import java.lang.reflect.*;

class Main {
    // Returns an array with the same contents but double in size.
    public static Object expand(Object array) {
        Object result = Array.newInstance(array.getClass().getComponentType(), 
            Array.getLength(array)*2);

        // Copy the old contents to the new array.
        for (int i=0; i<Array.getLength(array); i++) {
            Array.set(result, i, Array.get(array, i));
        }

        // A faster alternative would be
        //System.arraycopy(array, 0, result, 0, Array.getLength(array));

        return result;
    }

    public static void main(String[] args) {
        int[] ints = {5, 4, 6, 9, 1};
        char[] chars = {'j', 'a', 'v', 'a'};

        ints = (int[])expand(ints);
        chars = (char[])expand(chars);
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?