- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在不使用 Java 的 PQ 类来创建 PriorityQueue,并且 get
和 put
方法按预期工作,不幸的是 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());
也失败了,我根本没有得到。 gt
和e
是方法中定义的元素,无需进一步声明。
任何有助于改善我的 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/
如何使用 SPListCollection.Add(String, String, String, String, Int32, String, SPListTemplate.QuickLaunchO
我刚刚开始使用 C++ 并且对 C# 有一些经验,所以我有一些一般的编程经验。然而,似乎我马上就被击落了。我试过在谷歌上寻找,以免浪费任何人的时间,但没有结果。 int main(int argc,
这个问题已经有答案了: In Java 8 how do I transform a Map to another Map using a lambda? (8 个回答) Convert a Map>
我正在使用 node + typescript 和集成的 swagger 进行 API 调用。我 Swagger 提出以下要求 http://localhost:3033/employees/sear
我是 C++ 容器模板的新手。我收集了一些记录。每条记录都有一个唯一的名称,以及一个字段/值对列表。将按名称访问记录。字段/值对的顺序很重要。因此我设计如下: typedef string
我需要这两种方法,但j2me没有,我找到了一个replaceall();但这是 replaceall(string,string,string); 第二个方法是SringBuffer但在j2me中它没
If string is an alias of String in the .net framework为什么会发生这种情况,我应该如何解释它: type JustAString = string
我有两个列表(或字符串):一个大,另一个小。 我想检查较大的(A)是否包含小的(B)。 我的期望如下: 案例 1. B 是 A 的子集 A = [1,2,3] B = [1,2] contains(A
我有一个似乎无法解决的小问题。 这里...我有一个像这样创建的输入... var input = $(''); 如果我这样做......一切都很好 $(this).append(input); 如果我
我有以下代码片段 string[] lines = objects.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.No
这可能真的很简单,但我已经坚持了一段时间了。 我正在尝试输出一个字符串,然后输出一个带有两位小数的 double ,后跟另一个字符串,这是我的代码。 System.out.printf("成本:%.2
以下是 Cloud Firestore 列表查询中的示例之一 citiesRef.where("state", ">=", "CA").where("state", "= 字符串,我们在Stack O
我正在尝试检查一个字符串是否包含在另一个字符串中。后面的代码非常简单。我怎样才能在 jquery 中做到这一点? function deleteRow(locName, locID) { if
这个问题在这里已经有了答案: How to implement big int in C++ (14 个答案) 关闭 9 年前。 我有 2 个字符串,都只包含数字。这些数字大于 uint64_t 的
我有一个带有自定义转换器的 Dozer 映射: com.xyz.Customer com.xyz.CustomerDAO customerName
这个问题在这里已经有了答案: How do I compare strings in Java? (23 个回答) 关闭 6 年前。 我想了解字符串池的工作原理以及一个字符串等于另一个字符串的规则是
我已阅读 this问题和其他一些问题。但它们与我的问题有些无关 对于 UILabel 如果你不指定 ? 或 ! 你会得到这样的错误: @IBOutlet property has non-option
这两种方法中哪一种在理论上更快,为什么? (指向字符串的指针必须是常量。) destination[count] 和 *destination++ 之间的确切区别是什么? destination[co
This question already has answers here: Closed 11 years ago. Possible Duplicates: Is String.Format a
我有一个Stream一个文件的,现在我想将相同的单词组合成 Map这很重要,这个词在 Stream 中出现的频率. 我知道我必须使用 collect(Collectors.groupingBy(..)
我是一名优秀的程序员,十分优秀!