Java - gerneral

Extend an Array

To extend an Array in Java you must copy it to another one:


package com.sowas.snippets;
 
import java.lang.reflect.Array;
 
public class ResizeArray {
 
    public static void main(String[] args){
	Integer[] ints = {1, 2, 3, 4};
        System.out.println(ints.length);  // 4
        Object newArray = Array.newInstance(ints.getClass().getComponentType(), Array.getLength(ints)+2);  // +2
        System.arraycopy(ints, 0, newArray, 0, Array.getLength(ints));
        ints = (Integer[]) newArray;
        System.out.println(ints.length);  // 6
    }
}

Eigene Werkzeuge
Werkzeuge

gratis Counter by GOWEB
seit 9.10.2007