Results 1 to 4 of 4
- 02-22-2012, 02:08 AM #1
Member
- Join Date
- Feb 2012
- Posts
- 2
- Rep Power
- 0
¿Values by Reference? I dont understand...
First, sorry about my english :).
I have a question:
I have:
[...]
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(test2);
mostrar_array(test2);
}
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);
}
[...]
If I invoke "ordenar_array" and then I print "test2", the array dont have the initial values (435,544,33,22,4,23,54,12323,5,3,0,121,4,6), the array is sorted. ¿why? I dont return de array sorted to test2.
This example:
public class Test{
public static void sum (Integer i){
int val = i;
val+=4;
i = val;
}
public static void main (String args[]){
Integer i = new integer (5);
sum (i);
System.out.println(i);
}
}
Is the opposite, ¿why?
- 02-22-2012, 02:31 AM #2
Re: ¿Values by Reference? I dont understand...
Arrays are like objects. When you pass them as parameters to a method, the address of the array is passed, not a copy of the array. There is only one array and no copies, so any changes you make to the array are to the one array.
- 02-22-2012, 11:14 PM #3
Member
- Join Date
- Feb 2012
- Posts
- 2
- Rep Power
- 0
Re: ¿Values by Reference? I dont understand...
Thanks Norm !!
.gif)
With your explication, now I understand the difference ;).
- 02-22-2012, 11:27 PM #4
Similar Threads
-
HELP ...DONT UNDERSTAND Error
By ask4soteria in forum New To JavaReplies: 6Last Post: 11-26-2010, 09:43 AM -
i dont understand why i cant run my program please help
By kungfu in forum New To JavaReplies: 3Last Post: 07-28-2010, 02:49 PM -
8 questions I dont understand while studying for SCJP
By shankhas in forum Java CertificationReplies: 5Last Post: 05-19-2010, 07:53 AM -
Dont understand Return Statement.
By ocean in forum New To JavaReplies: 6Last Post: 10-22-2009, 12:06 PM -
Data Files - A problem that I dont understand :D
By Exhonour in forum New To JavaReplies: 7Last Post: 01-20-2009, 05:13 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks