meygraph
meygraph

Reputation: 21

When the use sort an array, fix and write private static void sort

Image1

My Main Code is :

public class Arrays {
    public static void main(String[] args) {
        //////////////// Sorting Arrays
        String [] castNames = new String [6];
        castNames[0] = "Zareyee Merila";
        castNames[1] = "Hosseini Shahab";
        castNames[2] = "Bayat Sareh";
        castNames[3] = "Peyman Moadi";
        castNames[4] = "Hatami Leila";
        castNames[5] = "Farhadi Sarina";
        Arrays.sort(castNames);
        for (int number = 0 ; number < 6 ; number++) {
            System.out.println(number + " : " + castNames[number]);
        }
    }
}

How can I fix this line of code:

Arrays.sort(castNames);

Without having to write this one:

private static void sort(String[] castNames) {
    // TODO Auto-generated method stub
}

Upvotes: 1

Views: 549

Answers (1)

bedwyr
bedwyr

Reputation: 5874

Okay, this question is a bit vague, but I expect you're just running into an issue with name collision.

You've named your class Arrays. This class has no static method called sort. There is, however, a utility in Java called java.util.Arrays which does implement a static sort method.

Your code is not calling the Java utility class, unless there's an import statement you haven't included.

Try changing the line to this: java.util.Arrays.sort(castNames);

Otherwise, you might consider renaming your Arrays class to something else.

Upvotes: 3

Related Questions