比较通用列表中的元素 [英] Comparing elements in a generic list

查看:119
本文介绍了比较通用列表中的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我今年早些时候为我的java类写了一个链表实现。这是一个名为LList的通用类。我们现在必须写出实验室的合并排序算法。我没有创建一个采用Ints的新List实现,而是决定重用之前创建的通用列表。



问题是如何比较两个通用对象?
java不会让我做类似的事情
$ b $ pre $ if(first.headNode.data> second.headNode.data)

所以,我的问题是,他们是一种实现某种比较函数的方法,任何类型的数据?我试过以下内容:

 字符串one,two; 
one = first.headNode.data.toString();
two = second.headNode.data.toString();
if(first.headNode.data.compareTo(second.headNode.data)< 0){
result.add(first.headNode.data);
//删除头节点。 remove()负责清单大小。
first.remove(1);
} else {
//如果compareTo返回0或更高,second.headNode会变小或
//等于first.headNode。因此,更新结果是安全的
// list
result.add(second.headNode.data);
second.remove(1);
}

甚至不能正常工作。我测试了数字6和12,上面增加了12的结果列表。



相关内容:

  private LList< T> mergeSort(LList< T>列表){
LList< T> first = new LList();
LList< T> second = new LList();
if(list.length()== 1){
return list;
}

int middle = list.length()/ 2;
second.headNode = list.getNodeAt(middle + 1);
second.length = list.length() - (middle);
//将前半部分设置为完整列表,然后删除后半部分。
first.headNode = list.headNode;
first.length = middle;
first.getNodeAt(middle).next = null;

//获取分割的一半。
first = mergeSort(first);
second = mergeSort(second);
返回合并(第一,第二);
}

私人的LList< T>合并(首先,第一次,第二次,第二次){
LList< T> result = new LList(); ((first.length> 0)&& amp;(second.length> 0)){
//好的,让强制toString比较的东西,因为泛型是一个疼痛。
字符串一,二;
one = first.headNode.data.toString();
two = second.headNode.data.toString();
if(one.compareTo(two))< 0){
result.add(first.headNode.data);
//删除头节点。 remove()负责清单大小。
first.remove(1);
} else {
//如果compareTo返回0或更高,second.headNode会变小或
//等于first.headNode。因此,更新结果是安全的
// list
result.add(second.headNode.data);
second.remove(1);
}
}
返回结果;

$ / code>

注意:可以找到整个的LList类[here]( b MD5:BDA8217D0756CC171032FDBDE1539478)

查看比较器可比较的界面。

您的排序方法应该使用 Comparator 或者你应该指定< T扩展了Comparable>,以便可以使用Comparable接口。

  public void sort(Comparable< T> comparator){
sort(SortType.MERGE,comparator);
}
....
私有的LList< T>如果(comparator.compare(first.headNode.data,second.headNode.data)< 0){$(< T> b $ b ...
}


I wrote a linked list implementation for my java class earlier this year. This is a generic class called LList. We now have to write out the merge sort algorithm for a lab. Instead of creating a new List implementation that takes Ints, I decided to just reuse the generic list I had created before.

The problem is how do I compare two generic objects? java wont let me do something like

if(first.headNode.data > second.headNode.data)

So, my question is, is their a way to implement some sort of comparison function that will work on any type of data? I tried the following:

        String one, two;
        one = first.headNode.data.toString();
        two = second.headNode.data.toString();
        if(first.headNode.data.compareTo(second.headNode.data) < 0) {
            result.add(first.headNode.data);
            // remove head node. remove() takes care of list size.
            first.remove(1);
        } else {
            // If compareTo returns 0 or higher, second.headNode is lower or
            // equal to first.headNode. So it's safe to update the result
            // list
            result.add(second.headNode.data);
            second.remove(1);
        }

Which wont even work properly. I tested with the numbers 6 and 12, the above adds 12 to the result list.

Relevant stuff:

 private LList<T> mergeSort(LList<T> list) {
    LList<T> first = new LList();
    LList<T> second = new LList();
    if (list.length() == 1) {
        return list;
    }

    int middle = list.length() / 2;
    second.headNode = list.getNodeAt(middle + 1);
    second.length = list.length() - (middle);
    // Set first half to full list, then remove the "second" half.
    first.headNode = list.headNode;
    first.length = middle;
    first.getNodeAt(middle).next = null;

    // Get the splitted halves.
    first = mergeSort(first);
    second = mergeSort(second);
    return merge(first, second);
}

private LList<T> merge(LList<T> first, LList<T> second) {
    LList<T> result = new LList();

    while((first.length > 0) && (second.length > 0)) {
        // Ok, lets force toString to compare stuff since generics are a pain.
        String one, two;
        one = first.headNode.data.toString();
        two = second.headNode.data.toString();
        if(one.compareTo(two)) < 0) {
            result.add(first.headNode.data);
            // remove head node. remove() takes care of list size.
            first.remove(1);
        } else {
            // If compareTo returns 0 or higher, second.headNode is lower or
            // equal to first.headNode. So it's safe to update the result
            // list
            result.add(second.headNode.data);
            second.remove(1);
        }
    }
    return result;
}

NOTE: entire LList class can be found [here](http://rapidshare.com/files/219112739/LList.java.html MD5: BDA8217D0756CC171032FDBDE1539478)

解决方案

Look into the Comparator and Comparable interfaces.

Your sort method should take Comparator or you should specify < T extends Comparable > so that the Comparable interface can be used.

public void sort(Comparable<T> comparator) {
    sort(SortType.MERGE, comparator);
}
....
private LList<T> merge(LList<T> first, LList<T> second) {
    ...
        if(comparator.compare(first.headNode.data, second.headNode.data) < 0) {
    ...
}

这篇关于比较通用列表中的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆