
{filelink=23}
import java.lang.reflect.Array;
public class UpdateArraySize {
public static void main (String[] args)
{
String tableau[]={"Lundi", "Mardi", "Mercredi", "Jeudi", "Vendredi"};
// Afficher la taille initiale
System.out.printf("Taille Initiale=>%s\nContenu=>%s",
tableau.length, Arrays.toString(tableau));
// Modifier la taille du tableau à 2 éléments
tableau=(String[])resizeArray(tableau, 2);
System.out.printf("Taille Après modification=>%s\nContenu=>%s",
tableau.length, Arrays.toString(tableau));
}
public static Object resizeArray(Object a, int newLength)
{
Class cl = a.getClass();
if (!cl.isArray()) return null;
int length = Array.getLength(a);
Class componentType = a.getClass().getComponentType();
Object newArray = Array.newInstance(componentType, newLength);
System.arraycopy(a, 0, newArray, 0, newLength);
return newArray;
}
}