微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

不兼容的类型找到“字符”,需要:“T”

如何解决不兼容的类型找到“字符”,需要:“T”

EDIT: As pointed out by dratenik,this was indeed a design error and it Could have easily been solved by removing any instances of T from my class and replacing them with chars and Characters where needed. I was stubborn though,and wanted to find a way to make it work with generics,and dratenik ALSO pointed out how to solve it (by sending instances of T to the method instead of trying to use chars directly),so huge thanks to them!

I don't kNow how to close questions or flag them as solved,but consider it solved! The new code is as follows:

public void fileDecoding(File oldFile,File newFile,PriorityQueue<T> pq) throws Exception{
    BufferedInputStream bis = new BufferedInputStream(new FileInputStream(oldFile));
    FileWriter fW = new FileWriter(newFile);
    if(newFile.canRead() && newFile.canWrite()) {
        root = new Node(null);
        convertCodetoTree(root,bis,pq);
        bis.read();
        decodeText(root,fW);
    }
    else throw new Exception("No permissions to read and write to file");
    fW.close();
    bis.close();
}
private void convertCodetoTree(Node node,BufferedInputStream bis,PriorityQueue<T> pq) throws Exception{
        int readChar = bis.read();
        if(readChar != -1 && (char) readChar != '\n'){
            if(readChar == '0'){
                node.left = new Node(null);
                convertCodetoTree(node.left,pq);
                node.right = new Node(null);
                convertCodetoTree(node.right,pq);
            }
            if(readChar == '1'){
                for(int i = 0; i < 8; i++) bis.read();
                node.data = pq.dequeue();
            }
        }
    }

THIS HAS BEEN SOLVED,but it might help someone with the same problem as me,so I'm not erasing it.

我有一项任务是使用同样从头开始创建的类和方法,从头开始创建霍夫曼编码的实现。

整个压缩部分进行得很顺利,但是在尝试编写解压测试文件方法时,我遇到了一个非常烦人的问题。我试着在google上搜索解决方法,也没有找到,所以决定写在这里。请原谅任何错误,英语不是我的第一语言。

(另外,快速编辑,我删除了 T extends Character。这是解决问题的失败尝试,并且没有奏效)

public class HuffmanBST<T extends Comparable<T>>{
class Node{
    private T data;
    private Node left,right;
    Node(T data){
        this.data = data;
        this.left = null;
        this.right = null;
    }
    Node(Node left,Node right){
        this.left = left;
        this.right = right;
    }
}
public void readFromFile(File file) throws Exception{
    BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
    BufferedReader br = new BufferedReader(new FileReader(file));
    String treeBinary = br.readLine();
    int i = 0;
    convertCodetoTree(root,treeBinary,i);
    decodeText(root);
    bis.close();
}
private void convertCodetoTree(Node node,String firstline,int index){    
char data = firstline.charat(index);
    if(data == 0) {
        node = new Node(null);
        convertCodetoTree(node.left,firstline,index++);
        convertCodetoTree(node.right,index++);
    }
    if(data == 1){
        String binaryToString = "";
        int i = 0;
        while(i < 8){
            index++;
            binaryToString += firstline.charat(index);
            i++;
        }
        char stringtochar = (char) Integer.parseInt(binaryToString,2);
        node = new Node(stringtochar); // This gives a compile-time error
    }
}

这是我编写的从已经压缩的文本文件重建霍夫曼树的方法,但它给了我一个编译时错误

'Node(T)' in 'HuffmanBST.Node' cannot be applied to '(char)'

我认为这是因为我的 HuffmanBST 类采用了一个泛型 T 并将其分配给节点的数据值,现在该节点不想被创建,因为它在运行之前无法确定 T 是什么。但我有另一种方法,它基本上做同样的事情,但它没有问题,也没有错误

public void buildTree(PriorityQueue<T> frequencies){
    PriorityQueue<Node> huffmanPQ = new PriorityQueue<>();
    while(!frequencies.isEmpty()) huffmanPQ.enqueue(frequencies.getPriority(),new Node(frequencies.dequeue())); // Doesn't throw a compile-time error.
    while(huffmanPQ.size() > 1){
        int weight1 = huffmanPQ.getPriority(); Node n1 = huffmanPQ.dequeue();
        int weight2 = huffmanPQ.getPriority(); Node n2 = huffmanPQ.dequeue();
        Node newNode = new Node(n1,n2);
        huffmanPQ.enqueue((weight1+weight2),newNode);
    }
    root = huffmanPQ.dequeue();
}

那我做错了什么?感谢您的帮助。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。