简体   繁体   English

当类型未知时,如何迭代Iterable对象?

[英]How do I iterate over an Iterable object when the type is not known?

For a homework assignment, I need to implement my own PriorityQueue and PriorityQueueSort. 对于家庭作业,我需要实现自己的PriorityQueue和PriorityQueueSort。 I used generics to get it working without the sort function, but now I'm stuck here.. 我使用泛型来使其工作而没有排序功能,但现在我被困在这里..

public static void PriorityQueueSort(Iterable<?> list, 
    PriorityQueue<?,?> pq) {
  if (!pq.isEmpty()) {
    throw new IllegalArgumentException("Non-Empty PriorityQueue");
  }

  for (Object obj : list) {

  }
}

I need to pass in a list and an empty PriorityQueue, so my best guess at how to do this is just above. 我需要传入一个列表和一个空的PriorityQueue,所以我最好猜测如何做到这一点就在上面。 How should I attack this so that I can iterate through the list with unknown type, and add each element in that list with the proper type into the priority queue? 我应该如何攻击这个以便我可以遍历未知类型的列表,并将具有正确类型的该列表中的每个元素添加到优先级队列中?


Edit: 编辑:

Here are a few more details since it was determined that I didn't include enough information. 由于确定我没有提供足够的信息,因此这里有一些细节。

I have a custom PriorityQueue class, and a custom Entry class that holds a key of type K, and a value of type V. 我有一个自定义的PriorityQueue类,以及一个自定义的Entry类,它包含一个类型为K的键,其值为V.

I need to be able to take any iterable list with any type T and iterate through it, taking each item and add it to an initially empty PriorityQueue as a key with null value. 我需要能够使用任何类型T获取任何可迭代列表并迭代它,获取每个项目并将其添加到最初为空的PriorityQueue作为具有空值的键。 I then need to continuously call removeMin() on my PriorityQueue and add it in order back into the same list object. 然后,我需要在我的PriorityQueue上连续调用removeMin()并将其按顺序添加回相同的列表对象。

public class PriorityQueue<K extends Comparable<? super K>,V> {

  private Entry<K,V> _head;
  private Entry<K,V> _tail;
  private int _size;

  public PriorityQueue() {
    this._head = null;
    this._tail = null;
    this._size = 0;
  }

  public int size() {
    return _size;
  }

  public boolean isEmpty() {
    return (size() == 0);
  }

  public Entry<K,V> min() {
    if (_head == null) {
      return null;
    }
    Entry<K,V> current = _head;
    Entry<K,V> min = _head;;

    while (current != null) {
      if (current.compareTo(min) < 0) {
        min = current;
      }
      current = current.getNext();
    }
    return min;
  }

  public Entry<K,V> insert(K k, V x) {
    Entry<K,V> temp = new Entry<K,V>(k,x);
    if (_tail == null) {
      _tail = temp;
      _head = temp;
    }
    else {
      _tail.setNext(temp);
      temp.setPrev(_tail);
      _tail = temp;
    }
    return temp;
  }

  public Entry<K,V> removeMin() {
    Entry<K,V> smallest = min();
    smallest.getPrev().setNext(smallest.getNext());
    smallest.getNext().setPrev(smallest.getPrev());

    return smallest;
  }

  public String toString() {
    return null;
  }

  public static <K> void PriorityQueueSort(Iterable<? extends K> list,
        PriorityQueue<? super K, ?> queue) {

      for (K item : list) {
          queue.insert(item, null);
      }

      list.clear();
  }

  public static void main(String[] args) {
    PriorityQueue<Integer, Integer> pq = 
        new PriorityQueue<Integer, Integer>();

    pq.insert(4, 2);
    pq.insert(5, 1);


    System.out.println(pq.min().toString());
  }
}

What you've got at the moment doesn't make sense in terms of the method signature - it would let you pass in a List<Button> and a PriorityQueue<String> for example. 你目前得到的东西在方法签名方面没有意义 - 它可以让你传递一个List<Button>和一个PriorityQueue<String>

I suspect you actually want something like: 我怀疑你真的想要这样的东西:

public static <T> void prioritySortQueue(Iterable<? extends T> iterable,
    PriorityQueue<? super T> queue) {

    for (T item : iterable) {
        queue.add(item);
    }
}

Note that the variance here just gives more flexibility - you could have a List<Circle> but a PriorityQueue<Shape> for example, and it's still type-safe. 请注意,这里的方差只是提供了更大的灵活性 - 例如,你可以有一个List<Circle>但是一个PriorityQueue<Shape> ,它仍然是类型安全的。

EDIT: Now that we have more details, I think you want something like this: 编辑:现在我们有更多的细节,我想你想要这样的东西:

public static <K> void prioritySortQueue(Iterable<? extends K> iterable,
    PriorityQueue<? super K, ?> queue) {

    for (T item : iterable) {
        queue.put(item, null);
    }
}

(Assuming you have a put method. We still don't know what your PriorityQueue class looks like.) (假设你有一个put方法。我们仍然不知道你的PriorityQueue类是什么样的。)

You need to make the method generic so that you can refer to the type: 您需要使方法通用,以便您可以引用类型:

public static <T> void PriorityQueueSort(Iterable<T> list, 
PriorityQueue<?,T> pq) {

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何迭代二叉树? - How do I iterate over Binary Tree? 如何遍历LinkedHashMaps的LinkedHashMap? - How do I iterate over a LinkedHashMap of LinkedHashMaps? 如何遍历类成员? - How do I iterate over class members? 我如何在vaadin中遍历手风琴? - how do i iterate over an accordion in vaadin? 我如何遍历 Jasper 中的列表? - How do I iterate over a list in Jasper? 如何使用RichFaces&#39;repeat&#39;标签在Java Iterable上进行迭代? - How can I iterate over java Iterable using RichFaces 'repeat' tag? 如何遍历JSONObject并通过已知的值找到ID,例如,我想获取值为“ТС-41”的ID,该怎么办? - How to iterate over JSONObject and found id by value which already known, for example I want to get id of value “ТС-41” how can I do it? 如何迭代 Either 的返回类型<ClientError, List<Employee> &gt;? - How do I iterate over return type of Either<ClientError, List<Employee>>? 如何覆盖Iterable <interface> 将类型方法转换为Iterable <? extends interface> 返回类型方法 - How do I Override a Iterable<interface> return type method into Iterable<? extends interface> return type method 非数组类型的对象如何迭代 - How to iterate through an object when it is not an array type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM