• Post Reply Bookmark Topic Watch Topic
  • New Topic

Transpose Method: cannot convert from Double to Double [][]  RSS feed

 
Dennis Von Valkenburgh
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I wrote the following method to transpose a matrix:


I cannot get it to work because I get an error on line 6 "Type mismatch: cannot convert from double to double[][]"
and again on line 8 "Type mismatch: cannot convert from double[][] to double.
I dont understand is temp not the same type as the m array?
Why is this error caused?
Thanks!
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, temp is the same type as the m array.
So temp is of type double[][];
But is it meant to be the type of the array, or the type of thing that you STORE in the array?

The error message is:
"Type mismatch: cannot convert from double to double[][]"
You have the line of code:


What is the type of m[rei][spa] ?

 
Dennis Von Valkenburgh
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stefan Evans wrote:Yes, temp is the same type as the m array.
So temp is of type double[][];
But is it meant to be the type of the array, or the type of thing that you STORE in the array?

The error message is:
"Type mismatch: cannot convert from double to double[][]"
You have the line of code:


What is the type of m[rei][spa] ?



I thought it is double [][] because the method receives it as such...
 
Campbell Ritchie
Marshal
Posts: 56581
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Go back to the original code and write down what the type of m is.
Don't use single‑letter identifiers, except for loop indices.
Don't use the = operator on arrays. You are applying the changes to the original array because both references point to the same object. Create a new array and copy and transpose the old array to it.
 
Campbell Ritchie
Marshal
Posts: 56581
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A few minutes ago, I wrote:Go back to the original code and write down what the type of m is.
. . .
That should have been m[0][0] (and temp[0][0]).
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
m is a double[][] - an array of array of doubles.

De-referencing the first index - i.e. m[0], you get a double[] - an array of doubles
De-referencing both indices e.g. m[0][0], you get a double.

The item you are 'swapping' is a double.
The temp variable should be the same type as the item you are swapping.
 
Campbell Ritchie
Marshal
Posts: 56581
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I still think OP should not swap anything in this method. I think creating a new array would be better; you can simply copy all the figures from the old array.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!