重新排列术语的arraylist以便Term('C',1),Term('H',1)Term('C',1),Term('H',1)Term('C',1),Term('H',1) turns into Term('C',1),Term('C',1),Term('C',1),Term('H',1),Term('H',1),Term('H',1)
到目前为止我的代码如下:我不想使用集合:
public void makeMolecular()
{
Term define = null;
char testAlpha = 'Z';
int testAtoms = 0;
int remember = 0;
Term replacer = null;
int increase = 0;
for(int j = 0; j < terms.size(); j++){
if (j < terms.size()){
for(int i = 0 + increase; i<terms.size(); i++){
Term termX = terms.get(i);
char charX = termX.getElement();
int intX = termX.getAtoms();
if (charX < testAlpha){
testAlpha = charX;
testAtoms = intX;
define = termX;
remember = i;
}
else {
continue;
}
}
terms.remove(remember);
terms.add(0,define);
increase = increase+1;
//System.out.println(terms);
}
else {
break;
}
}
//System.out.println(terms);
}
这个当前代码不断打印正确的第一次迭代,但在此之后填充。非常感谢任何帮助。
答案 0 :(得分:1)
假设您的Term对象具有代码的访问者....
public class CustomComparator implements Comparator<MyObject> {
@Override
public int compare(MyObject o1, MyObject o2) {
return o1.getTermCode() > (o2.getTermCode();
}
}
然后用类似
的方式调用它Collections.sort(yourList, new CustomComparator());