This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

returning an array

 
Kevin Knowles
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do I return an array?
Is it
return (a[][]);
or is it
return (a);
?
 
Blake Minghelli
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
use return a;
You use the backets only when declaring an array (e.g. String a[][] = new String[5][5]) or working with a specific element in an array (e.g. a[1][2] = "test" or String s = a[1][2]).
When in doubt, just try compiling it.
 
Kevin Knowles
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, thanks, but I get this errror
when using
return (a);

..........java:74: incompatible types
found : double[][]
required: double
return(a);
^
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What are you declaring that you return? You need to do something like this:
public double[][] myMethod() {
double[][] a;
.....
return a;
}
And when you invoke the method:
double[][] b = myMethod();
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic