Arranging by Alphabetical Order

时光总嘲笑我的痴心妄想 提交于 2019-12-04 05:10:42

问题


I'm new to Java and I'm trying to arrange an arrayList of terms in alphabetical order. (A term is defined as a char and an int) (e.g. {Term('Z',4),Term('C',3),Term('Q',2) ...} )

My code is as follows:

    public Term nextElement()
{
    Term max = terms.get(0);
    char maxtest = max.getElement();
    for (int i = 1; i < terms.size(); i++){
        Term tester = terms.get(i);
        char maxtest2 = tester.getElement();
        if (maxtest2 > maxtest) {
            tester = max;
        }
    }
    return max;
}

Why isn't this working? and how do I accomplish this? My arrayList is called term filled with type Term


回答1:


Your problem with this line of Code. Your class is not a Type of Comparable So, On which property or criteria compareTo() method will compare these two objects ???

   res = maxtest.compareTo(maxtest2); //Your maxtest object is not Comparable Type.

You must need to make your class Term Comparable type. and , Override the method compareTo() as per your need.

You have not mentioning the variable's or structure of your class Term . So, I am assuming that your class have such kind of Structure .

public class Term implements Comparable<Term> {
    private Character alpha;
   private int number;
   //getter and setters +Constructors as you specified
   ....
    ....
     ...
      .....
//  Now Set a criteria to sort is the Alphanumeric.
    @Override
    public int compareTo(Term prm_obj) {
        if (prm_obj.getAlpha() > this.alpha) {
            return 1;
        } else if (prm_obj.getAlpha() < this.alpha) {
            return -1;

        } else {
            return 0;
        }

    }

Now Your Class become a comparable Type. So you may apply Collections.sort(Collection obj) which automatically sort your ArrayList<Term>.

Here I write a demo for this.

public static void main(String... args){

    List<Term> obj_listTerm = new ArrayList<>();
     //add all the data you given in question
    obj_listTerm .add(new Term('Z', 4)); 
    obj_listTerm .add(new Term('Q', 2));
    obj_listTerm .add(new Term('c', 3));

    // print without Sorting your Term ArrayList.
    System.out.println("This is the list unsorted: " + myTermList);
    // Sort Using Collections.sort() Method.

    Collections.sort(myTermList);

    // After applying sort() you may see your Sorted ArrayList.
    System.out.println("This is the list SORTED: " + myTermList);
}



回答2:


You can use the Collection class and sort the list of term you have, you need only to make the class Term comparable

Example:

public class Term implements Comparable<Term> {
    .....
    //  .....
    //  criteria to sort is the char
    @Override
    public int compareTo(Term o) {
        if (o.getLetter()> this.letter) {
            return 1;
        } else if (o.getLetter() < this.letter) {
            return -1;

        } else {
            return 0;
        }

    }

public static void main(String[] args) {
    // test
    List<Term> myTermList = new ArrayList<>();
    myTermList.add(new Term('Z', 4));
    myTermList.add(new Term('Q', 2));
    myTermList.add(new Term('c', 3));
    // check how the look like
    System.out.println("This is the list unsorted: " + myTermList);
    // now sort them
    Collections.sort(myTermList);
    // check how the look like
    System.out.println("This is the list SORTED: " + myTermList);
}

Edit>

if you dont want to implement comparable then modify this:

res = maxtest.compareTo(maxtest2);

because this is not valid since maxtest and maxtest2 are primitives and not objects...

use instead

res = Character.compare(maxtest, maxtest2);

and then use the result to verify your logic and make decisions:

if (res >1) {
            System.out.println("bigger");

        }else if (res<1) {
            System.out.println("smaller");

        }else {
            System.out.println("same");

        }


来源:https://stackoverflow.com/questions/36564264/arranging-by-alphabetical-order

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!