0

我有一个充满对象的数组列表。每个对象由两个整数组成。如何根据第一个整数对数组列表进行排序,同时将第二个整数与其原始整数保持一致?然后如何添加排序数组列表的所有第二个整数?

我试过这个:

Collections.sort(info, new Comparator()
    {
        public int compare(M one, M two) 
        {
            return m1.getCost().compareToIgnoreCase(m2.getCost());
        }

    });

class M{
//Declares the attributes belonging to this class
private int money;
private int cost;

//Constructor method
{
    //this refers to object in which the method was called
    this.cost = cost;
    this.money = money;
}

//Returns the cost variable
public int getCost()
{
    return cost;
}

public void setCost(int cost)
{
    this.cost = cost;
}

//Returns the maximum amount
public int getMoney()
{
    return money;
}

public void setMoney(int Money)
{
    this.Money = Money;
}

}

我是java新手,所以任何帮助将不胜感激(:

4

3 回答 3

2
import java.util.*;

public class M implements Comparator<M>{

int i,j;

M(int a, int b){
    i=a; j=b;
}

public int compare(M m1, M m2){

    return (m1.i-m2.i);
}

public boolean equals(Object o){
    if(this.i==((M)o).i)
        return true;
    else 
        return false;
}


public static void main(String args[]){

        M m1 = new M(2,1);
        M m2 = new M(3,2);
        M m3 = new M(1,3);

        ArrayList<M> a = new ArrayList<M>();
        a.add(m1);
        a.add(m2);
        a.add(m3);

                   Collections.sort(a,(Comparator<M>)m1);

                    //ArrayList a is sorted
            for(int j=0;j<a.size();j++){
        System.out.println(a.get(j).i);
    }
}
}

在您的类中实现 Comparator 接口,以便 Collections.sort 方法使用类 M 的 int1 对对象进行排序。然后使用 Collections.sort 方法对数组进行排序。这应该回答问题的第一部分。

于 2013-01-02T06:34:43.383 回答
1

创建Comparator时,应将其用作 Generic 类型T(其中T表示数组中的对象),因此:public int compare(T a, T b) { .. }.

例如:

new Comparator<M> {
    // Note the signature
    public int compare(M one, M two) 
    {
        // Now return a correct value based on the ordering
        // using one.getCost() and two.getCost()
        // Obviously "compareToIgnoreCase" is wrong for use with numbers.
        // Either work this out or see one of the existing/linked answers.
    }
}

如果不使用泛型,则签名是public int compare(Object one, Object two)并且可能需要强制转换 - 请注意,没有重载(int, int); 这是一个从未使用过的声明方法!

也可以看看:

于 2013-01-02T05:22:18.367 回答
1

这个怎么样 :

public class Main {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    M[] mm = new M[4];
    mm[0] = new M(11, 2);
    mm[1] = new M(11, 4);
    mm[2] = new M(4, 67);
    mm[3] = new M(4, 2);
    mm = compareM(mm);
    for (int a = 0; a < mm.length; a++) {
        System.out.println("index : "+a+" a : "+mm[a].a+" b : "+mm[a].b);
    }
}

public static M[] compareM(M[] data) {
    for (int a = 0; a < data.length - 1; a++) {
        for (int b = a + 1; b < data.length; b++) {
            if (data[a].a > data[b].a) {
                M temp = data[a];
                data[a] = data[b];
                data[b] = temp;
            }
        }
    }
    for (int a = 0; a < data.length; a++) {
        int indStart = a;
        int indEnd = a;
        for (int b = a + 1; b < data.length; b++) {
            if (data[b].a == data[a].a) {
                indEnd++;
            } else {
                b = data.length;
            }
        }
        a = indEnd;
        for (int c = indStart; c <= indEnd; c++) {
            for (int d = c + 1; d <= indEnd; d++) {
                if (data[c].b > data[d].b) {
                    M temp = data[c];
                    data[c] = data[d];
                    data[d] = temp;
                }
            }
        }
    }
    return data;
}

static class M {

    public int a, b;
    //u can have function to set value of a n b, or any function

    public M(int ax, int bx) {
        this.a = ax;
        this.b = bx;
    }
}

}

于 2013-01-02T05:39:17.233 回答