通用数组列表冒泡排序问题 [英] Generic arraylist bubblesort problems

查看:38
本文介绍了通用数组列表冒泡排序问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有内置例程,但作为学习者,我想使用我自己的设备进行排序,而且由于排序是旧帽子,我决定尝试制作我自己的通用排序例程如果我弄清楚它们在 Java 中的工作方式,我可以使用数字或字符串,甚至日期.

I know there are builtin routines, but as a learner, I want to sort using my own devices, and since sorting is old hat, I decided to try to make my own generic sort routine that I could use for numbers or strings and maybe even dates, if I ever figure out how they work in Java.

所以这就是我所拥有的,直到现在我已经将一个错误换成了另一个错误,我只有两个地方有错误(用**"标记括起来),需要弄清楚如何进行比较.

So here's what I have, having traded one error for another for another until now I only have errors in two places (enclosed within "**" markers), with need to figure out how to compare.

package sort;
import java.util.ArrayList;

public  abstract class Sort<E> implements Comparable<E> {

   public void swap(ArrayList<E> a, int i, int j) {
    E c = a.get(i);
    a.set(i,a.get(j));// = a[j];
    a.set(j, c);
  }

  public void bubbleSort(ArrayList<E> a) {
    boolean inOrder = false;
    while (!inOrder) {
      inOrder = true;
      for (int i = 1; i < a.size(); i++) {
        **if( a.get(i - 1).compareTo(a.get(i)) > 0 )** {
//cannot find symbol: method compareTo(E); location: class Object
//where E is a type-variable: E extends Object declared in class Sort                 
      inOrder = false;
          swap(a, i, i - 1);
        } 
      }
    }
  }

  public static void main(String args[]) //hadda lose 'static' for 'setLayout' to work
  {
    ArrayList<Integer> ary = new ArrayList<>();
    ary.add(2); ary.add(4); ary.add(7); ary.add(3);
    **bubbleSort(ary)**;
//method bubbleSort in class Sort<E> cannot be applied to given types; 
//required: ArrayList<E>
//found: ArrayList<Integer>
//reason: actual argument ArrayList<Integer> cannot be converted to ArrayList<E> 
//by method invocation conversion where E is a type-variable:
//E extends Object declared in class Sort
    for (int i = 0; i < ary.size(); i++) {
      System.out.println(ary.get(i));
    }
  }

  @Override
  public int compareTo(E o) {
    **return 0;** // fixing errors above may help this fall into place
  }
}

我正在尝试学习我感觉准备好的东西,却发现我还没有完全准备好;关闭,无雪茄.

I'm trying to learn things that I feel ready for only to find that I'm not quite ready; close, cigarless.

推荐答案

这个:

public  abstract class Sort<E> implements Comparable<E> {

表示 E 是任意对象类型,并且 Sort 的实例可以与 E 的实例进行比较.(因此您的错误消息抱怨 E.compareTo 不存在,因为 Object 没有这样的方法.)您想要的是:

means that E is an arbitrary object type, and that instances of Sort<E> can be compared to instances of E. (So your error message is complaining that E.compareTo doesn't exist, since Object doesn't have such a method.) What you want is this:

public abstract class Sort<E extends Comparable<E>> {

这意味着 E 必须是一个实例可以相互比较的类型.

which means that E must be a type whose instances can be compared to each other.

编辑添加:实际上,正如 SLaks 共同指出的那样,Sort 没有真正的理由是通用的;你只需要 bubbleSort 方法是通用的.此外,正如 MadProgrammer 暗示的那样,Sort 应该是非 abstract(所以你可以直接实例化它)或者 bubbleSort 应该是 static(因此可以在不实例化 Sort 实例的情况下调用它)或两者兼而有之.例如:

Edited to add: Actually, as SLaks together point out, there's no real reason for Sort to be generic; you just need the bubbleSort method to be generic. Further, as MadProgrammer implies, either Sort should be non-abstract (so you can instantiate it directly) or bubbleSort should be static (so it can be called without instantiating a Sort instance) or both. For example:

public class Sort {
    private static <E> void swap(ArrayList<E> a, int i, int j) {
        ...
    }

    private static <E extends Comparable<E>> void bubbleSort(ArrayList<E> a) {
        ...
    }

    ...
}

更好的是,Sort 可以是一个带有 sort 方法的接口,而 BubbleSort.sort(...) 只是一个实现它(而不是给 Sort 一个特定的 bubbleSort 方法).

Better yet, Sort can be an interface with a sort method, and BubbleSort.sort(...) is just an implementation of it (rather than giving Sort a specific bubbleSort method).

这篇关于通用数组列表冒泡排序问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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