I am trying to write a method in java that will allow me to swap the rows of a 2d array of the double type. My code works perfectly when I use it on a 2d array of ints, but it doesn't work when applied to double. Am I missing something fundamental about double data types? Any insight is greatly appreciated.
// swapRows
// double[][] Integer -> double[][]
public static double[][] swapRows(double[][] a, int i)
{
double[] temp = a[i];
for(; i < a.length - 1; i++)
a[i] = a[i+1];
a[a.length - 1] = temp;
return a;
}
// version 2 with new 2d array
public static double[][] swapRows(double[][] a, int i)
{
double[][] b = new double[a.length][a[0].length];
double[] temp = a[i];
for(int k = 0; k < i;k++)
b[k] = a[k];
for(; i < a.length - 1; i++)
b[i] = a[i+1];
b[b.length - 1] = temp;
return b;
}
// note that the rows don't just swap, the specified row i is sent to the bottom of the array, and every other row shifts up // for the 2d array:
{ {3, 5, 6},
{2, 1, 1},
{9, 0, 4} }
// I expect that when i = 0, the method return the 2d array :
{ {2, 1, 1},
{9, 0, 4},
{3, 5, 6} }
// but instead I get:
{ {0.0, -29.999996, -38.571428},
{18.999997, 0.0, 0.999999},
{18.0, 29.999996, 36.0} }
// when I use int instead of double, I get the expected array
iis an input parameter.