Java - Returning an array from a method to main -


would glad if can me following problem. following method suppose returns array ith entry number of times int appeared in array a. method name gives use of method. drawing histogram of data found in array a.

    public static int histogram(int m, int[] a){          int[] b = new int[m];          (int = 0; < m; i++){             int w = 0;               (int j = 0; j < a.length; j++){               if (a[j] == i){              w++;             }         }             b[i] = w;        }     return b;  } 

accoring me code correct - may wrong - can't test because i've got error thats bugging me.

histogram.java:22: incompatible types found : int[] required: int return b;

1) above mean?? on example on internet , in handbook use "return b;" return array main program.

2) how return function work? able use following code print values of array b? because @ moment error array b not being initialised in main... think error go away if error 1 sorted out. i'm new java won't know - new programming too.

for (int x = 0; x < m; x++){          system.out.printf("%d ", b[x]);       }` 

3) should maybe print values in histogram() method? question answer though "return" array histogram values in it.

would glad if can help...

if you've come far - lot... reading till here means lot :)

you should try returning array of ints instead of single integer

public static int[] histogram ... 

i'm not sure 2&3, though.

2) return not function, it's keyword causing leave method.
3) can, if want to.

edit
whatever variables declared in histogram method stay there, they're local. if want variable in main method, declare it. e.g.,

int[] b = histogram(param1, param2); 

Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -