0

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

6
  • 2
    you should include the code for the array swap Commented May 14, 2018 at 14:27
  • 2
    Where is your code ? Commented May 14, 2018 at 14:27
  • Well, of course they don't just swap: you're not swapping them. What's the problem? Commented May 14, 2018 at 14:36
  • There is a missing "i" : ` for(; i < a.length - 1; i++)` my bad, he is.. Commented May 14, 2018 at 14:36
  • @xoxel. Not sure I agree with you. i is an input parameter. Commented May 14, 2018 at 14:37

2 Answers 2

1

As the code is correct IMHO, the 2D array must be corrupt (unlikely, same double[] row object in two rows), or much more likely the fact that you are altering the passed array, and returning it too, might indicate some erroneous handling of the result.

double[][] a = ...
double[][] b = swapRows(a, i);
// a == b

A version that is creates a copy that is changed:

public static double[][] swapRows(double[][] a, int i) {
    double[][] result = new double[a.length][];
    for (int j = 0; j < a.lnength; ++j) {
        result[j] = Arrays.copyOf(a[j], a[j].length);
    }

    double[] temp = result[i];
    for(; i < result.length - 1; i++) {
        result[i] = result[i+1];
    }
    result[result.length - 1] = temp;
    return result;
}

double[][] a = { { 1.0, 2.0 }, { 3.0, 4.0 } };
double[][] b = swapRows(a, 0);
assert b[0][0] == 3.0 && b[0][1] == 4.0 && b[1][0] == 1.0 && b[1][1] == 2.0;
System.out.println("result = " + Arrays.deepToString(result));
Sign up to request clarification or add additional context in comments.

4 Comments

I think you're right that it might be the array. Because when I use the array { [ 7 12 6]; [8 2 0]; [2 1 0]} . I get the expected return except that all values are negative where they should be positive.
And that the swapRows alters the passed parameter, a? Looks like a systematic error, like double[][] b = negate(a); (and now also a is negated) and such.
I added a second version that uses a new array so that the input has a different reference than the output. However, I still get the same result. Do you have any techniques for identifying where the systematic error is?
The error probably lays just at a different spot. I have added test code.
0

I've identified the problem. I had other test lines in my main function that were modifying the value of the array before it was passed into the swapRows method. Thank you for you assistance.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.