Java - ¿Por referencia / por valor?.

 
Vista:

¿Por referencia / por valor?.

Publicado por David (1 intervención) el 22/02/2012 02:00:36
Hola, tengo una duda,

Si yo tengo este código

[...]
static long tiempo = 0;
public static void main(String[] args)
{
int test2 [] = {435,544,33,22,4,23,54,12323,5,3,0,121,4,65};
ordenar_array_Burbuja(test);
}

public static void ordenar_array_Burbuja(int array_a_ordenar[]) // Función que devuelte un array ordenado
{
mostrar_array(array_a_ordenar);
tiempo = System.currentTimeMillis();
int n = array_a_ordenar.length;
for (int pass=1; pass < n; pass++) { // count how many times
// This next loop becomes shorter and shorter
for (int i=0; i < n-pass; i++) {
if (array_a_ordenar[i] > array_a_ordenar[i+1]) {
// exchange elements
int temp = array_a_ordenar[i]; array_a_ordenar[i] = array_a_ordenar[i+1]; array_a_ordenar[i+1] = temp;
}
}
}
long total = 0;
total = System.currentTimeMillis()-tiempo;
System.out.println("He tardado "+ total +" en terminar");
mostrar_array(array_a_ordenar);
}
[...]

Si luego muestro "test2", no tiene el valor inicial (435,544,33,22,4,23,54,12323,5,3,0,121,4,65), esta ordenado, ¿porqué se modifica el valor de test2?. ¿este es así siempre?.
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

¿Por referencia / por valor?.

Publicado por reiky (60 intervenciones) el 01/03/2012 19:40:18
por que realmente estas ordenando a test2

1.- int test2 [] = {435,544,33,22,4,23,54,12323,5,3,0,121,4,65};

2.- ordenar_array_Burbuja(test);

3.- public static void ordenar_array_Burbuja(int array_a_ordenar[])

"test2" se lo envias a un metodo "ordenar_array_Burbuja" por tanto "array_a_ordenar" = "test2" , los 2 objetos apuntan al mismo bloq de memoria
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar