سؤال

I want to write a compare class so that I can sort a list of elements using this comparator later on, but i get an error. The code looks as such:

class Elem {
            public String user_name;
            public String given_name;


            public String getGivenName() {
                return given_name;
            }
        }

        class Compare implements Comparator<Elem> {
            public int compare(Elem o1, Elem o2) {
                return o1.getGivenName().compareTo(o2.getGivenName());
            }
        }

This gives me the error:

Elem cannot be resolved to a type

I'm not sure how I can fix this error.

هل كانت مفيدة؟

المحلول

It means that the compiler cannot find class Elem.

Don't forget to import it at the top of your java class in case the Elem class is in the different package.

Also make sure you don't have any typo.

نصائح أخرى

The code above compiles cleanly at my machine (Java 7) with the added import.

import java.util.Comparator;

class Elem {
    public String user_name;
    public String given_name;


    public String getGivenName() {
        return given_name;
    }
}

class Compare implements Comparator<Elem> {
    public int compare(Elem o1, Elem o2) {
        return o1.getGivenName().compareTo(o2.getGivenName());
    }

}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top