gpt4 book ai didi

java - 如何阻止该循环更改值?

转载 作者:行者123 更新时间:2023-12-02 09:31:10 25 4
gpt4 key购买 nike

我正在尝试对已创建的对象数组实现堆排序。它旨在根据其 eventTime 属性进行排序,但在队列排序时,每个 eventTime 属性都设置为与其他属性相同。有没有办法解决这个问题,以便它按升序创建“优先级队列”?

主要:

public static void main(String[] args) {

CustomerQueue cQ = new CustomerQueue(0, 0, false);

for (int i = 0; i < 10; i++) {
CustomerQueue cQ1 = new CustomerQueue(0, 0, false);
cQ.enqueue(cQ1);
System.out.println(cQ1.arrivalTime);
}

System.out.print("\n");

PriorityQueue pQ = new PriorityQueue(0, 0, 0, false);

while (!cQ.isEmpty()) {
for (CustomerQueue c : cQ.array) {
PriorityQueue pQ1 = new PriorityQueue(0, 0, 0, false);
pQ1.eventTime = c.arrivalTime;
pQ.enqueue(pQ1);
System.out.println(pQ1.eventTime);
cQ.dequeue();
}
}
pQ.sort(pQ.array);
System.out.println(Arrays.toString(pQ.array));
}

优先队列:

class PriorityQueue {

PriorityQueue array[] = new PriorityQueue[10];
private int front;
private int rear;
private int count;
private int eventType;
double eventTime;
private double tallyTime;
private boolean paymentMethod;

public PriorityQueue(int evT, int eT, double tT, boolean pM) {
this.eventType = evT;
this.eventTime = eT;
this.tallyTime = tT;
this.paymentMethod = pM;
front = 0;
rear = -1;
count = 0;
}

public void enqueue(PriorityQueue pQ) {
if (isFull()) {
System.out.println("OverFlow\nProgram Terminated");
System.exit(1);
}

rear = (rear + 1);
array[rear] = pQ;
count++;
}

public void sort(PriorityQueue arr[]) {
int n = arr.length;

// Build heap (rearrange array)
for (int i = n / 2 - 1; i >= 0; i--) {
heapify(arr, n, i);
}

// One by one extract an element from heap
for (int i = n - 1; i >= 0; i--) {
// Move current root to end
int temp = (int) arr[0].eventTime;
arr[0] = arr[i];
arr[i].eventTime = temp;

// call max heapify on the reduced heap
heapify(arr, i, 0);
}
}

void heapify(PriorityQueue arr[], int n, int i) {
int largest = i; // Initialize largest as root
int l = 2 * i + 1; // left = 2*i + 1
int r = 2 * i + 2; // right = 2*i + 2

// If left child is larger than root
if (l < n && arr[l].eventTime > arr[largest].eventTime) {
largest = l;
}

// If right child is larger than largest so far
if (r < n && arr[r].eventTime > arr[largest].eventTime) {
largest = r;
}

// If largest is not root
if (largest != i) {
int swap = (int) arr[i].eventTime;
arr[i] = arr[largest];
arr[largest].eventTime = swap;

// Recursively heapify the affected sub-tree
heapify(arr, n, largest);
}
}

public void dequeue() {
if (isEmpty()) {
System.out.println("UnderFlow\nProgram Terminated");
System.exit(1);
}

front = (front + 1) % array.length;
count--;
}

public boolean isEmpty() {

return (size() == 0);
}

public int size() {
return count;
}

public boolean isFull() {
return (size() == array.length);
}

public PriorityQueue peek() {
if (isEmpty()) {
System.out.println("UnderFlow\nProgram Terminated");
System.exit(1);
}
return array[front];
}

@Override
public String toString() {
return eventType + " " + eventTime + " " + tallyTime + " " + paymentMethod;
}

}

最佳答案

我看到两个最有可能错误的部分以及问题的原因:

heapify 方法中,您有:

int swap = (int) arr[i].eventTime;
arr[i] = arr[largest];
arr[largest].eventTime = swap;

sort 方法中,您有:

int temp = (int) arr[0].eventTime;
arr[0] = arr[i];
arr[i].eventTime = temp;

假设您想在数组内部切换 CustomerQueue 对象,它们应该是这样的:

// In the heapify:
CustomerQueue swap = arr[i];
arr[i] = arr[largest];
arr[largest] = swap;

// In the sort:
CustomerQueue temp = arr[0];
arr[0] = arr[i];
arr[i] = temp;

您的代码当前在伪代码中的作用是:

CustomerQueue ith<​​ 例如 name = "I", eventTime = 1
例如,CustomerQueue largestname = "largest", eventTime = 2

int temp = (int) arr[i].eventTime; // temp is now 1
arr[i] = arr[largest]; // Both arr[i] AND arr[largest] are now CustomerQueue largest (with name = "largest" and eventTime = 2)
arr[i].eventTime = temp; // Both arr[i] AND arr[largest] are now changed to eventTime temp (which is 1)

因此,现在 arr[i]arr[largest] 都将保持:name = "largest"且 eventTime = 1,如下以及引用相同的 CustomerQueue 实例。

关于java - 如何阻止该循环更改值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57956184/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com