
{filelink=12428}
import java.lang.reflect.Array;
public class TableauUtils {
public static void main (String arg[]) {
String s[] = new String[20];
System.out.println("La taille du tableau s est " + s.length);
// 20
s = (String[])TableauUtils.etendre(s);
System.out.println("La nouvelle taille du tableau s est " + s.length);
// 30
int i[] = {1 ,2 ,3, 4, 5};
System.out.println("La taille du tableau i est " + i.length);
// 4
i = (int[])TableauUtils.etendre(i);
System.out.println("La nouvelle taille du tableau i est " + i.length);
// 6 }
public static Object etendre(Object a) {
Class cl = a.getClass();
if (!cl.isArray()) return null;
int length = Array.getLength(a);
int newLength = length + (length / 2);
// Ajouter 50% de plus à la taille
Class componentType = a.getClass().getComponentType();
Object newArray = Array.newInstance(componentType, newLength);
System.arraycopy(a, 0, newArray, 0, length);
return newArray;
}
}