0

我的程序打印主构造函数,但不返回 3 个底部函数,而是返回它的编译。我哪里错了?

import java.util.*;

public class Main {
  public static void main(String[] args) {
    PriorityQueue<String> queue1 = new PriorityQueue<String>();
    System.out.println(" ");
    //Adding strings to queue1
    queue1.offer("Goerge");
    queue1.offer("Jim");
    queue1.offer("John");
    queue1.offer("Blake");
    queue1.offer("Kevin");
    queue1.offer("Michael");
    System.out.println("Priority queue using Comparable:");

    while (queue1.size() > 0) {
    System.out.print(queue1.remove() + " ");
    }

    PriorityQueue<String> queue2 = new PriorityQueue<String>(4,
    Collections.reverseOrder());
    System.out.println(" ");
    //Adding strings to queue2
    queue2.offer("George");
    queue2.offer("Katie");
    queue2.offer("Kevin");
    queue2.offer("Michelle");
    queue2.offer("Ryan");
    System.out.println("\nPriority queue using Comparator:");

    while (queue2.size() > 0) {
    System.out.print(queue2.remove() + " ");
    }
  }

    public static PriorityQueue<String> union(PriorityQueue<String> queue1, PriorityQueue<String> queue2){
    PriorityQueue<String> queue3 = new PriorityQueue<>();
    queue3.addAll(queue1);
    queue3.addAll(queue2);
    return queue3;
    }

    public static PriorityQueue<String> intersection(PriorityQueue<String> queue1, PriorityQueue<String> queue2) {
    PriorityQueue<String> queue3 = new PriorityQueue<>(queue1);
    queue3.retainAll(new HashSet<>(queue2));
    return queue3;
    }

    public static PriorityQueue<String> difference(PriorityQueue<String> q1, PriorityQueue<String> intersectionQueue) {
    PriorityQueue<String> q3 = new PriorityQueue<>(q1);
    q3.removeAll(new HashSet<>(intersectionQueue));
    return q3;
    }
}

这 3 个函数必须返回 2 个队列的并集、差集和交集。但它们没有显示在输出中。

4

2 回答 2

1

我猜两件事,

一:正如评论所指出的,你没有调用函数,在这种情况下,只需从你的 main 方法调用它们。

二:在 while 循环中打印输出后,您在 queue1 和 queue2 上都调用 union。在您的打印语句中,您正在从队列中删除值,因此当您调用联合函数时,两个队列都是空的,因此没有输出。如果您需要打印数据使用

System.out.println(Arrays.toString(priorityQueue.toArray()));

而不是从优先级队列中删除元素

于 2019-08-01T15:09:25.890 回答
0
package priorityQueues;

import java.util.Arrays;
import java.util.HashSet;
import java.util.PriorityQueue;

/**
 * @author felendernukeri
 *
 */
public class UnionDifferenceIntersection {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // Create two priority queues
        PriorityQueue < String > pq1 = new PriorityQueue < > (Arrays.asList(
            "George", "Jim", "John", "Blake", "Kevin", "Michael"));

        PriorityQueue < String > pq2 = new PriorityQueue < > (Arrays.asList(
            "George", "Katie", "Kevin", "Michelle", "Ryan"));
        
        
        
        // Display the two sets and union, difference, and intersection
        System.out.println("Set 1: " + pq1);
        System.out.println("Set 2: " + pq2);
        System.out.println("Union: " + findUnion(pq1, pq2));
        System.out.println("Difference: " + findDifference(pq1, pq2));
        System.out.println("Intersection: " + findIntersection(pq1, pq2));
        
        // Initiate hashSet
        HashSet<String> hashSet = new HashSet<>();

    }
    
    /** Calculating Union. */
      public static <E> PriorityQueue<E> findUnion(PriorityQueue<E> p1, PriorityQueue<E> p2) {
            PriorityQueue<E> union = new PriorityQueue<>(p1);
            union.addAll(p2);
            return union;
        }
      
    /** Calculating difference. */
      public static <E> PriorityQueue <E> findDifference(PriorityQueue<E> p1, PriorityQueue<E> p2){
            PriorityQueue<E> difference = new PriorityQueue<>(p1);
            difference.removeAll(p2);
            return  difference;
        }
      
        /** Calculating Intersection. */
      public static <E> PriorityQueue <E> findIntersection(PriorityQueue<E> p1, PriorityQueue<E> p2){
            PriorityQueue<E> intersection = new PriorityQueue<>(p1);
            intersection.retainAll(p2);
            return  intersection;
        }

}
于 2020-07-21T09:34:44.277 回答