扩展链接列表方法不在单独的类main方法中工作

时间:2013-02-15 05:41:31

标签: java methods linked-list

这是扩展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);



}

我在输出中得到[]而不是我想要的[first,1]。我是否需要在PQ类中创建构造函数?

2 个答案:

答案 0 :(得分:0)

好男孩们,让我们帮助这个纯洁的家伙吧。将项添加到列表的更正确的实现将如下所示:

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推荐的链接以及评论中提供的建议。通过数据结构的基础课程也不会受到影响。

答案 1 :(得分:0)

这是有效的:

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>

感谢你的帮助,尽管人们真的很感激它:)