按字母顺序从ArrayList返回元素

时间:2016-04-12 07:10:10

标签: java loops for-loop arraylist while-loop

我正在研究一个程序来操作化学公式,我正在编写一个方法,需要循环一个名为“terms”的ArrayList并按字母顺序返回第一个。

e.g。 terms = {Term('H',4),Term('C',2),Term('H',4),Term('C',1)}将返回Term('C',2)< / p>

到目前为止,我已编写此代码,但它无效。我是Java语言的真正初学者。

public Term nextElement()
{
    int i = 0;
    for (i = 0; i < terms.size()-1; i++)
    {
        int j = 1;
        while (i + j <= terms.size())
        if (terms.get(i).getElement() > terms.get(i+j).getElement())
        {
            terms.remove(i+j++);
            return terms.get(i);
        }
    }
    return null;
}

我很感激任何想法或建议来解决这个问题。谢谢!

5 个答案:

答案 0 :(得分:3)

这里有两个选项:

  1. 让您的Term类实现Comparable接口并覆盖其compareTo()方法。然后,您可以使用Collections.sort(listOfTerms)对它们进行排序并循环。
  2. 添加实现TermComparator接口的类Comparator,使用Collections.sort(listOfTerms, new TermComparator())并遍历排序列表。

答案 1 :(得分:0)

使用Collections.sort(terms);它将按字母顺序排列列表。

答案 2 :(得分:0)

这是你需要做的:

List<Term> terms = //your list
Collections.sort(terms, new Comparator<Term>() {
    @Override
    public int compare(Term t1, Term t2) {
        return t1.getElement().compareTo(t2.getElement());
    }
});

答案 3 :(得分:0)

<强> CODE:

public class CodeSample {


public static void main(String[] args) {
    List<Term> terms=new ArrayList<Term>();
    terms.add(new Term('H',4));
    terms.add(new Term('C',2));
    terms.add(new Term('H',4));
    terms.add(new Term('C',1));
    System.out.println("Before Sorting");
    for(Term term:terms){
        System.out.print(term.toString().concat(" "));
    }
    Collections.sort(terms,new Comparator<Term>() {
        @Override
        public int compare(Term object1, Term object2) {                
            if (object1.getElement() != object2.getElement()) {
                return object1.getElement() - object2.getElement();
            } else {
                return object2.getCount() - object1.getCount();
            }           
        }
    });
    //Sorted terms
    System.out.println("After Sorting");
    for(Term term:terms){
        System.out.print(term.toString().concat(" "));
    }
}

public static class Term{
    private char element;
    private int count;
    public Term(char element, int count) {
        super();
        this.element = element;
        this.count = count;
    }
    public char getElement() {
        return element;
    }
    public int getCount() {
        return count;
    }
    @Override
    public String toString() {
        return "Term [element=" + element + ", count=" + count + "]";
    }       
}
 }

<强>输出:

Before Sorting
Term [element=H, count=4] Term [element=C, count=2] Term [element=H, count=4] Term [element=C, count=1] 
After Sorting
Term [element=C, count=2] Term [element=C, count=1] Term [element=H, count=4] Term [element=H, count=4] 

答案 4 :(得分:0)

1-您可以实施Comparable并覆盖compareTo()

int compareTo(Object obj){
Term term = (Term)obj;
if(term.getElement < this.getElement())
  return 1;
else if (term.getElement == this.getElement())
  return 0;
else
  return -1;
}

然后使用

Collection.sort(terms);

2-

 public Term nextElement()
    {
        char minElement = 'Z';
        int index = 0;
        for (int i = 0; i < terms.size(); i++)
        {
            if (terms.get(i).getElement() < minElement)
            {
                minElement = terms.get(i).getElement();
                index = i;
            }
        }
        Term temp = terms.get(index);
        terms.remove(index)
        return temp;
    }