Anonymoose
Anonymoose

Reputation: 143

Why won't this Java array's .sort method work?

Starting to think I'm using the wrong book for learning. I've copied this word-for-word from Sam's Learn Java, but the .sort(names); method is undefined for type of array.

I have a feeling it's to do with the public static void main(String[] args) { call, but I don't know how to amend it.

package arrays;

import java.util.*;

public class Arrays {
    public static void main(String[] args) {
        String names[] = { "Lauren", "Audrina", "Heidi", "Whitney",
                "Stephanie", "Spencer", "Lisa", "Brody", "Frankie", "Holly",
                "Jordan", "Brian", "Jason" };
        System.out.println("The original order:");
        for (int i = 0; i < names.length; i++) {
            System.out.print(i + ": " + names[i] + " ");
        }
        Arrays.sort(names);
        System.out.println("\nThe new order:");
        for (int i = 0; i < names.length; i++) {
            System.out.print(i + ": " + names[i] + " ");
        }
        System.out.println();
    }
}

Upvotes: 4

Views: 12533

Answers (2)

Osama Awaad
Osama Awaad

Reputation: 1

package again.data;

import java.util.Arrays;
import java.util.Scanner;

/**
 *
 * @author Osama
 */
public class AgainData {

    /**
     * @param args the command line arguments
     */
    @SuppressWarnings("empty-statement")
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);


        System.out.print(" Enter tour Element = ");
        int size = sc.nextInt();
        int arr[] = new int[size];
        for (int i = 0; i < arr.length; i++) {
            System.out.println("Enter your  number  = " + i + "  =   ");
            arr[i] = sc.nextInt();
        }
        System.out.print("sort number trtub tsoduan : ");
        for (int i = 0; i <arr.length; i++) {
            for (int j =i+1; j <arr.length; j++) {
                if(arr[i]>arr[j]){
                    int temp=arr[i];
                    arr[i]=arr[j];
                    arr[j]=temp;
                }
              
            }
        }



        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");

        } // print sort number

Upvotes: -2

T.J. Crowder
T.J. Crowder

Reputation: 1074138

(Thomas pointed to the answer, but as it was a comment you can't accept it; here's a CW you can accept.)

Arrays is a class in the java.util package, which has a sort method. By calling your own class Arrays, you hide the java.util one from your code (this is sometimes called "shadowing"), so the Arrays.sort(names); line is trying to use a sort method on your class (and it doesn't have one).

To fix it, you have three options:

  1. Change the name of your class (ArrayTest, whatever), or

  2. Change your call to sort to: java.util.Arrays.sort(names); (so the compiler knows you're talking about java.util.Arrays, rather than your class), or

  3. Use import static java.util.Arrays.sort; to import the method sort rather than the class Arrays and invoke it with sort(names); (without a preceding class name).

Upvotes: 10

Related Questions