Today, while writing a simple program, I found some strange behaviour. I wrote simple sorting method in which returns another sorted array.
public double[] sortMe(double[] array)
{
double[] narray=new double[array.length];
narray=array;
for(int i=0;i<narray.length;i++)
{
for(int j=0;j<narray.length;j++)
{
if(narray[i]<narray[j])
{
double temp=narray[i];
narray[i]=narray[j];
narray[j]=temp;
}
}
}
// TODO Auto-generated method stub
return narray;
}
In my driver class, when I called this sortMe method, it updated my testArray as well. Since testArray is out of scope for sortMe method, then when testArray got sorted ?
double [ ] testArray = {3, 6, 2, 5, 8, 4, 1, 7};
double [ ] results;
results = af.sortMe(testArray);
af.printMe(results);