这是一个扩展LinkedList的类,我正在使用addtopq创建一个优先级que系统,其中que的第一个元素是String,第二个元素是与该字符串相关的优先级。忽略异常
class PQ extends LinkedList {
public void addtopq(String s, Integer p){
if (p<1 || p>20) throw new InvalidPrioty("Priority number must be between 1 and 20");
int pos = 0;
int k = 0;
int i = 1;
int j = 0;
LinkedList nlist = new LinkedList();
if (this==null){
addLast(s);
addLast(p);
System.out.println(this);
}
}
else {
while (i<size()){
int x = Integer.valueOf(get(i).toString());
if (p > x) pos = 1;
else if (p==x){
pos = 0;
break;
}
else if (p < x) {
pos = -1;
break;
}
}
if (pos==1){
addLast(s);
addLast(p);
}
if (pos==-1||pos==0){
for (k=0; k<(i-1); k++) nlist.add(j, get(j));
nlist.addLast(s);
nlist.addLast(p);
for (k=k+1 ; k<size(); k++) nlist.add(get(k));
}
}
}
}
这是拒绝将2个新对象添加到列表中的主文件:
public class Main {
public static void main(String[] args) {
PQ list = new PQ();
list.addtopq("first", 1);
System.out.println(list);
}
我在输出中得到[],而不是我想要的[第一,1]。我需要在PQ类中创建一个构造函数吗?
这是有效的:
public class Main {
public static void main(String[] args) {
PQ list = new PQ();
//Insert test code here:
list.addtopq("First",20);
list.addtopq("Second",3);
list.addtopq("Third", 1);
list.addtopq("Forth",3);
list.addtopq("Fifth",2);
list.addtopq("Sixth",4);
list.tostring();
import java.util.LinkedList;
class InvalidPrioty extends RuntimeException{
public InvalidPrioty(String s){
super(s);
}
}
class PQ extends LinkedList{
public void addtopq(String s, Integer p){
if (p<1 || p>20) throw new InvalidPrioty("Priority number must be between 1 and 20");
System.out.println("Adding "+'"'+s+'"'+" with priority "+p);
int pos = 0;
int k;
int i = 1;
PQ nlist = new PQ();
//The list will initially be empty so the first call the the addtopqmethod
// will add the objects to the queue and avoid errors when checking if objects values
// are greater than null.
if (isempty()){
addLast(s);
addLast(p);
}else {
while (i<size()){
// Creates an object to see whether the priority in the item being added is higher
// than the one in the queue already.
Integer x = Integer.valueOf(get(i).toString());
//The loop will break if priority is less than or equal to x so that it can place it behind the items
//with higher or the same priority. I assumed that as long as all the strings in the list with the same
//priority were together, that it wouldn't matter what order they in.
if (p > x) pos = 1;
else if (p<=x){
pos = 0;
break;
}
i=i+2;
}
//Adds to the end of the que as it has the largest priority
if (pos==1){
addLast(s);
addLast(p);
}
if (pos==0){
//Adds everything up to the one being replaced into a seperate list...
for (k=0; k<(i-1); k++) nlist.add(k, get(k));
//Adds the new String with its priority into the List...
nlist.addLast(s);
nlist.addLast(p);
//Adds the rest of the items
for (; k<size(); k++) nlist.add(get(k));
//Clears the list and adds the new order
clear();
addAll(nlist);
nlist.clear();
}
}
}
public boolean isempty(){
if (contains(null)) return true;
else return false;
}
Output:
Adding "First" with priority 20
Adding "Second" with priority 3
Adding "Third" with priority 1
Adding "Forth" with priority 3
Adding "Fifth" with priority 2
Adding "Sixth" with priority 4
Queue Contents: <"Third":1,"Fifth":2,"Forth":3,"Second":3,"Sixth":4,"First":20>
谢谢你的帮助,尽管伙计们真的很感激:)
将项目添加到列表的更正确的实现如下所示:
import java.util.LinkedList;
class PQ extends LinkedList {
public void addtopq(String s, Integer p) {
addLast(s);
addLast(p);
}
public static void main(String[] args) {
PQ list = new PQ();
list.addtopq("first", 1);
System.out.println(list);
}
}
看看你在代码中犯了多少基本错误,我建议浏览Luiggi Mendoza推荐的链接和评论中给出的建议。学习数据结构的基础课程也不会有什么坏处。