Diario de un Aprendiz de Java

Tuesday, June 21, 2005

Jugando con arrays

Enunciado
As an exercise, qrite a method called cloneArray that takes an array of integers as parameter, creates a new array that is the same size, copies the elements from the first array into the new one, and then returns a reference to the new array.

Codigo
class Array {
  public static int[] cloneArray(int[] aCopiar) {
   int[] copiado = new int[aCopiar.length];
   for(int contador = aCopiar.length; contador > 0; contador --) {
    copiado[contador - 1] = aCopiar[contador - 1];
   }
   return copiado;
  }

  public static void main(String[] args) {
   int[] unArray = new int[4];
   unArray[0] = 12;
   unArray[1] = 24 / unArray[0];
   unArray[2] = unArray[1] * 3;
   unArray[3] = 5;
   int[] otroArrayIgual = cloneArray(unArray);
   System.out.println("Esto es igual a 5 ... " + otroArrayIgual[3]);
  }
}

12 Comments:

Post a Comment

<< Home