Arranging by Alphabetical Order

前端 未结 2 766
庸人自扰
庸人自扰 2021-01-24 06:20

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(\

相关标签:
2条回答
  • 2021-01-24 06:27

    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");
    
            }
    
    0 讨论(0)
  • 2021-01-24 06:32

    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);
    }
    
    0 讨论(0)
提交回复
热议问题