Ver Mensaje Individual
  #2 (permalink)  
Antiguo 07/02/2011, 06:00
linkghost
 
Fecha de Ingreso: febrero-2011
Mensajes: 27
Antigüedad: 13 años, 3 meses
Puntos: 1
Respuesta: Crear Nombre Variable Dinamicamente

Código java:
Ver original
  1. public static void Main () {
  2.    int[] a = {1,2,3};
  3.    a = (int[])RedimencionarArray(a,5);
  4.    a[3] = 4;
  5.    a[4] = 5;
  6.    for (int i=0; i<a.Length; i++)
  7.       System.Console.WriteLine (a[i]);
  8. }

aqui nos tirara NullPointerException... por razones obvias nos caemos del array

pero llamamos a una segundo metodo
Código java:
Ver original
  1. public static System.Array RedimencionarArray(System.Array ArrayViejo, int TamanoNuevo) {
  2.    int TamanoViejo= ArrayViejo.Length;
  3.    System.Type tipoElemento= ArrayViejo.GetType().GetElementType();
  4.    System.Array ArrayNuevo= System.Array.CreateInstance(tipoElemento,TamanoNuevo);
  5.    int preserveLength = System.Math.Min(TamanoViejo,TamanoNuevo);
  6.    if (preserveLength > 0)
  7.       System.Array.Copy (ArrayViejo,ArrayNuevo,preserveLength);
  8.    return ArrayNuevo;
  9.  }

y tenemos arrays dinamicos