2

I am an amateur in Java and I am stuck at returning and array in Java. Here is my program (shortened):

public class Mershor {

    public static int[] shotrting(int a[]) {
        //here I wrote an algorithm for shorting 
        return a;
    }

    public static void main(String[] args) {
        int[] a;
        a = new int[6];
        a[0] = 5;
        a[1] = 3;
        a[2] = 4;
        a[3] = 7;
        a[4] = 1;
        a[5] = 2;
        for (int o = 0; o <= a.length; o++) {
            System.out.println(shotrting(a[o]));    
        }
    }
}

Error:

The method shotrting(int[]) in the type Mershor is not applicable for the arguments (int)

1
  • 2
    @ParkerHalo o is an int variable, so that part does work Commented Nov 30, 2015 at 15:00

2 Answers 2

3

You are passing only integers, not array to shotrting(int[]).

To pass the entire array you type as argument the name of array: shotrting(a).

I also did some minor changes to your main program.

  public static void main(String[] args) {

    //shorter initialization
    int[] a = new int {5, 3, 4, 7, 1, 2};

    int[] result_array=shotrting[a];

    //To print the array in a more appropriate form.
    System.out.print(java.util.Arrays.toString(result_array);

}
Sign up to request clarification or add additional context in comments.

Comments

1

Change

System.out.println(shotrting(a[o]));    

to

System.out.println(shotrting(a)[o]);    

The shotrting takes an array as a parameter, not an Integer.

a = [5, 3, 4, 7, 1, 2]

a[o] = 5.....

shotrting(a) returns an array. You can access its elements using the [index] notation, like shotrting(a)[0], shotrting(a)[1]...

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.