English 中文(简体)
将链接列表与特定对象变量排序吗?
原标题:Sorting LinkedList with specific variable of object?

我有一个泡泡排序函数, 可以用“ a” 变量命令 SomeObj 链接列表列表。 如果我想同时用“ b” 变量排序列表, 那么如何? 如何用 b 来代替写入另一个函数?

public static void BubbleSort(LinkedList<SomeObj> objs) {
    int len = objs.size();

    for(int pass = 1; pass < len; pass++) {
        for (int i=0; i < len - pass; i++) {
            if(objs.get(i).a > objs.get(i + 1).a) {
                SomeObj p = objs.get(i);
                objs.set(i,objs.get(i+1));
                objs.set(i + 1, p);
            }
        }
    }
}
最佳回答

Implement Comparator interface in a class with its compare method. that accepts two objects and compares them which returns -ve,0,+ve values to tell less than , equal or greater than.

创建此比较器的一个对象, 并将其传递到泡沫排序方法, 让它使用比较方法比较这两个对象 。

您的天体应该拥有所有这些字段的梯子 。

每当您想更改对象 s 的比较标准时, 使用不同的比较器 。

请选中此 < a href=>" "http://www.java Developmenter.co.in/java-example/java-comparator-example.html" rel="nofollow" >example 。

问题回答

您会让 SomeObj 执行 可比较 接口, 然后使用该接口与它互动 。

如果您能用第一个“a”来排序,那么“b”就是在您的 SomeObj 类中执行可比较的比较课程,并进行比较。

决定哪个是最高优先级。 检查高优先级变量 - 如果它们不同, 排序就像您只是根据它进行排序。 如果高优先级变量匹配, 那么您必须返回到低优先级变量, 并以此为基础进行整个比较 。

if (obj1.getA() != obj2.getA()) {
    // do compare on As
    // e.g. return onj2.getA() - obj1.getA()
}
else { // A s match, so do compares on B
    // e.g. return obj2.getB() - obj1.getB()
}

我认为最简单的方法就是交换变量值而不是列表项目。使用这种方法,您可以同时以不同的方式“排序”列表。

我猜测有某种误解 - 它不是关于排序 一个然后B, 它是一种2列表之一, 用户想要分类 两者都独立, 但不创建第二个列表, 或者我是不是不对?

用于对列表进行排序

Collections.sort(List list, Comparator c));

类似此主方法中的类似方法

class Pair{
    int a;
    int b;

    public Pair(int a, int b) {
        this.a=a;
        this.b=b;
    }

    public String toString() {
        // TODO Auto-generated method stub
        return "["+a+","+b+"]";
    }

    //test
    public static void main(String[] args) {
        Comparator<Pair> comparatorA=new Comparator<Pair>() {
            @Override
            public int compare(Pair o1, Pair o2) {
                if (o1.a>o2.a) return 1;
                if (o1.a<o2.a) return -1;
                return 0;
            }
        };
        LinkedList<Pair> list=new LinkedList<>();
        list.add(new Pair(1,2));
        list.add(new Pair(2,1));
        list.add(new Pair(3,1));
        list.add(new Pair(1,3));

        Collections.sort(list, comparatorA);
        System.out.println(list);
    }
}

现在您可以对 b 值进行比较, 并使用该比较器的收藏. sort 。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...