• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem: Returning an array from method "Matrix Addition"  RSS feed

 
Omar Perez
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greetings friends,

I come here to ask for your advice and guidance about an issue that I'm having with the results from the addition of two array coming from a method, using objects: The returning value that I''m getting from the object whenever i print it is : [[I@2738ef1d

Here is the code the method's code

• Detailed description of problem
o what you did
I created a code that will perform the addtion of two matrices, by using invoking a method from a new object.
o what you expected to happen
I expected the object to return the values and print them from the main class.
o why you expected it to happen
Because I wrote Return in the Addition class’s method.
o why you need it to happen
Because I need to print the values from there.
o what actually happened instead and
nothing happened, it returned nothing, except
o why what happened is a problem to you
• Error message
• none
• Version numbers of relevant libraries, JDK, tools, operating system etc.
• NETBEANS 7.2, JAVA SE7
• Code snippets







• Stack trace
none
• What else you tried/what worked/what didn't work
Tried to return the values from the method by using Arrays.deepToString() and Arrays.ToString methods.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java's array classes don't override Object's toString() method, and println(X) eventually calls X.toString() for non-null, non-primitive values. So you're getting Object's version, which prints out the class name and hashCode.

You can either iterate over the arrays yourself, printing each element along the way, or you can use java.util.Arrays.toString() (for 1D arrays) ro Arrays.deepToString() (for arrays containing other arrays, which is what a multi-D array in Java is).
 
Omar Perez
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:Java's array classes don't override Object's toString() method, and println(X) eventually calls X.toString() for non-null, non-primitive values. So you're getting Object's version, which prints out the class name and hashCode.

You can either iterate over the arrays yourself, printing each element along the way, or you can use java.util.Arrays.toString() (for 1D arrays) ro Arrays.deepToString() (for arrays containing other arrays, which is what a multi-D array in Java is).


Greetings, tried that and didn't work.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Omar Perez wrote:
Greetings, tried that and didn't work.


Then you did something wrong.

If you'd like a more detailed response than that, perhaps you could TellTheDetails(←click) about exactly what you tried (using an SSCCE(←click)) and exactly what "didn't work" means.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Both of Jeff's suggestions definitely do work. Can you show what you tried so we can explain where you went wrong.
 
Omar Perez
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Docherty wrote:Both of Jeff's suggestions definitely do work. Can you show what you tried so we can explain where you went wrong.


Well now is giving incompatible type error INT/STRING

the method is returning ints no String and when it did return something, it returned some kind of 2*2 extended matrix, which should not be the outcome of a 3*3. , just by using Arrays.deepToString(resultmatrix) method, remeber that is multidimensional matrix. and this method shows support only for unidemensional matrix.
 
sai rama krishna
Ranch Hand
Posts: 536
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
>>Arrays.deepToString() (for arrays containing other arrays, which is what a multi-D array in Java is).


I tried recently. It did work for multi dimentional array. Not sure why it is not displaying for you.
 
Paul Clapham
Sheriff
Posts: 22843
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Omar Perez wrote:just by using Arrays.deepToString(resultmatrix) method, remeber that is multidimensional matrix. and this method shows support only for unidemensional matrix.


Of course it does. That's because Java in fact doesn't have any such thing as multi-dimensional arrays. It only supports one-dimensional arrays. However, it's possible for the elements of a one-dimensional array to be arrays themselves, and you could interpret such an array-which-contains-arrays as a multi-dimensional array.

And the Arrays.deepToString() method knows that, and behaves accordingly.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Omar Perez wrote:
Tony Docherty wrote:Both of Jeff's suggestions definitely do work. Can you show what you tried so we can explain where you went wrong.


Well now is giving incompatible type error INT/STRING


Then you're still doing something wrong. Not seeing your code, not seeing the exact, complete error message, not seeing an indication of which line of code is causing that error, it's hard to say what you're doing wrong.
 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Everything suggested must work, you're probably doing something wrong. Listen to people, post the code you're having issue with so others can point you to the right direction.

I just hope you're not gonna implement separate class for each operation on matrices.
 
Omar Perez
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Detailed information in the original post.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And what exact problem are you having? Please TellTheDetails(←click).

Although I can tell you right off the bat you need to look more closely ad the docs for deepToString(), both what arguments it takes and what it actually does.
 
Omar Perez
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Detailed information posted in the original post, hope it be good enough.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Detailed information posted in the original post, hope it be good enough.

Please don't go back and change a much earlier post, if you do this the thread no longer makes any sense to anyone else.

The description you have added is good but you still haven't shown how you tried to use deepToString() so we can't say for sure why it wasn't working - why are you so reluctant to show the code we are asking for?

Tried to return the values from the method by using Arrays.deepToString() and Arrays.ToString methods.

This could be your problem. You can't return a String from the matrixAddition() method as it is declared to return int[][].
 
Omar Perez
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all folks for your patience and help, finally made it. God Bless.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!