变量即使重置也不会改变其值

时间:2019-03-08 17:15:49

标签: java computer-science

老实说,我不知道问题是什么。同一问题发生在代码的另一部分中,尚未修复。 该项目将基于一个3个整数的客户对象的到达时间,合并两个所有准备好的排序队列。这三个整数是到达时间,服务时间和退出时间。
它仅打印917次7次。  这是代码。

public static Queue2<Customer> Question3(Queue2<Customer> A, Queue2<Customer> B) {

    Queue2<Customer> C = new Queue2<Customer>(); 


    if (A.getSize() == 0 && B.getSize() == 0) { 

        System.out.println("Sorry Bro");  

    } else if (A.getSize() == 0) { 

        while (B.getSize() > 0) {

            C.enqueue(A.dequeue()); 

        }

    } else if (B.getSize() == 0) { 

        while (A.getSize() > 0) { 

            C.enqueue(B.dequeue()); 

        }

    } else { 
        Customer c1 = A.dequeue(); 
        Customer c2 = B.dequeue(); 
        int n = A.getSize() + B.getSize(); 

        for (int x = 0; x < n; x++) { 

            if (A.getSize() == 0) {

                C.enqueue(B.dequeue()); 

            } else if (B.getSize() == 0) { 

                C.enqueue(A.dequeue()); 

            } else { 

                if (c1.getArrival() > c2.getArrival()) { 

                    C.enqueue(c2); 
                    c2 = B.dequeue(); 
                    System.out.println(c2.getArrival()); 
                } else { 

                    C.enqueue(c1); 
                    c1 = A.dequeue(); 
                    System.out.println(c1.getArrival()); 
                }

            }

        }

    } 

     return C; 
} 

这是主要方法:

 Queue2<Customer> ctest = new Queue2<Customer>(); 
    ctest.enqueue(new Customer(915,10,0)); 
    ctest.enqueue(new Customer(916,6,0)); 
    ctest.enqueue(new Customer(918,7,0)); 
    ctest.enqueue(new Customer(925,3,0)); 

    Queue2<Customer> ctest1 = new Queue2<Customer>(); 
    ctest1.enqueue(new Customer(917,10,0)); 
    ctest1.enqueue(new Customer(922,3,0)); 
    ctest1.enqueue(new Customer(924,2,0)); 

    Queue2<Customer> ctest3 = Question3(ctest,ctest1); 
    Customer c4 = ctest3.dequeue(); 

    while(ctest3.getSize() > 0) { 

       System.out.println(c4.getArrival()); 
       c4 = ctest3.dequeue(); 
       System.out.println("Printing"); 

    }
    System.out.println(c4.getArrival()); 

2 个答案:

答案 0 :(得分:1)

不清楚您的原始问题,如果我们实施Queue2,将会很有帮助。

您无需考虑有关代码的任何事情,

else if (A.getSize() == 0) { 

        while (B.getSize() > 0) {

            C.enqueue(A.dequeue()); 
            // A is already empty, why you dequeue it ? 
            // same goes with the other else (may be you wanted to do C.enqueue(B.dequeue())

        }
    }

然后

Customer c1 = A.dequeue(); // dequeue usually reduce the size of the queue
Customer c2 = B.dequeue(); 
int n = A.getSize() + B.getSize(); // This `n` might not be the one you expected 

答案 1 :(得分:0)

我认为您在尺寸上的逻辑是否正确。

当您使用df = df.merge(pa, on="PaperID") df = df.groupby('Main_Author')['Co_Author'].apply(lambda x: tuple(x)).reset_index() df['Num_Co_Authors'] = df['Co_Author'].apply(lambda x: len(x)) 检查A是否为空时,然后从A(其为空,然后检查B的大小是否已更改)出队。

除非A和B是同一个队列,否则从A出队不会改变B的大小。