按字母顺序排列

Arranging by Alphabetical Order

我是 Java 的新手,我正在尝试按字母顺序排列术语数组列表。 (术语定义为 char 和 int)(例如 {Term('Z',4),Term('C',3),Term('Q',2) ...} )

我的代码如下:

    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;
}

为什么这不起作用?我该如何做到这一点? 我的 arrayList 被称为 term filled with type Term

您可以使用集合 class 并对您拥有的术语列表进行排序,您只需要使 class 术语 可比

示例:

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);
}

编辑>

如果你不想实现可比性,那么修改这个:

res = maxtest.compareTo(maxtest2);

因为这是无效的,因为 maxtest 和 maxtest2 是原语而不是对象...

改用

res = Character.compare(maxtest, maxtest2);

然后使用结果来验证你的逻辑并做出决定:

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

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

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

        }

你这行代码的问题。您的 class 不是 Comparable 的类型,因此,属性 或标准 compareTo() 方法将 compare 这两个对象???

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

您必须需要使您的 class Term 可比类型。并且,根据需要覆盖方法 compareTo()

您没有提及 class Term 的变量或结构。所以,我假设你的class有这样的结构。

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;
        }

    }

现在你的 Class 变成了 comparable 类型。因此,您可以应用 Collections.sort(Collection obj),它会自动 sort 您的 ArrayList<Term>

这里我写个demo。

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);
}