gpt4 book ai didi

java - 如何将已实现的 PriorityQueue 作为字符串打印?

转载 作者:行者123 更新时间:2023-12-01 21:56:41 25 4
gpt4 key购买 nike

我正在不使用 Java 的 PQ 类来创建 PriorityQueue,并且 getput 方法按预期工作,不幸的是 toString() 方法没有通过我的测试用例,我似乎无法在控制台上打印任何内容。我很不知道问题是什么,因为 get() 和 put() 方法工作正常,但给出的多个测试用例没有通过。我的代码:


private int priority;
private String data;

Element(int priority, String data) {
// Ihr Code
this.priority = priority;
this.data = data;

}

public String getData() {
// Ihr Code
return data;
}


public int getPriority() {
// Ihr Code
return priority;
}

/**
* Return data and priority as string
* Format: Data (Priority)
* e.g: abc (7)
*/
public String toString() {
String str = data + " " + Integer.toString(priority) + ")";
return str;
}
}


public class PriorityQueue {


static final int SIZE = 32;


private Element[] data = null;

// actual number of entries
private int len = 0;


/**
* Creates a new PriorityQueue
*/
public PriorityQueue() {
data = new Element[SIZE];
}

/**
* Adds a new element into the queue as long as there is space
* Returns true if element could be added, otherwise false
*/
boolean put(Element element) {
// Ihr Code
if (len == SIZE) {
return false;
}else if (len > 0 && len < SIZE){
int i = len;
while (i > 0 && element.getPriority() > data[i-1].getPriority()){
data[i] = data[i-1];
i--;
}
data[i] = element;
len++;

return true;
}else{
return false;
}

}

/**
* Returns element with the highest priority
* and removes it from the queue. Otherwise returns null
*
*/
Element get() {
// Ihr Code
if(len==0){
return null;
}else if (len > 0){
Element x = data[0];
for(int i = 1; i < len; i++){
data[i-1] = data[i];
}
--len;
return x;
}else{
return null;
}
}

/**
* Number of entries
*/
int length() {
// Ihr Code
return len;
}

/**
* Returns contents of the queue as a String
* Format: data1 (priority1), data2 (priority2)
* e.g: abc (7), cde (8)
* Attention: There should be no comma at the end of the String
*/
public String toString() {
// Code
StringBuilder sb = new StringBuilder();
for (int i = 0; i < data.length; i++){
sb.append(data[i]).append(",");
}
if(sb.length() > 0){
sb.deleteCharAt(sb.length()-1);
}
String res = sb.toString();

return res;
}

我可以很好地编译程序,但没有按预期打印任何内容。我的测试用例例如

        pq.put(new Element(3, "hello"));
pq.put(new Element(7, "world"));
String pqAsString = pq.toString().replace(" ", "");
assertTrue(pqAsString.contains("hello"));

法官说 assertTrue 失败。此外,另一个测试用例 assertEquals(gt.getPriority(), e.getPriority()); 也失败了,我根本没有得到。 gte是方法中定义的元素,无需进一步声明。

任何有助于改善我的 PQ 或帮助我使用 toString 方法的帮助,我们将不胜感激!谢谢。

最佳答案

在您的方法 put() 中,您还需要一个 else if block :

} else if(len==0) {
data[0] = element;
len++;
}

我将按如下方式重新组织此方法。更具可读性。

public boolean put(Element element) {
// Ihr Code
if(len >= SIZE) {
return false;
}else {
data[len]=element;
Element temp;
for(int i=len; i>0; i--) {
if(data[i].getPriority()>data[i-1].getPriority()) {
temp=data[i];
data[i]=data[i-1];
data[i-1]=temp;
i++;
}
}
len++;
return true;
}
}

您还可以将 if block 添加到 toString() - 打印输出会更漂亮

for (int i = 0; i < data.length; i++){
if(data[i]!=null) {
sb.append(data[i]).append(",");
}
}

关于java - 如何将已实现的 PriorityQueue 作为字符串打印?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58738101/

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