java - 我更新到 jdk 11,现在我的代码抛出错误,我不知道为什么,这对我来说都是正确的
问题描述
我刚刚在 vscode 上更新到 jdk 11,它抛出了我以前从未见过的错误。我真的很确定我的代码是正确的,但它谈论的是错位的构造函数和东西,我就是不明白。
import java.util.PriorityQueue;
import java.util.Arrays;
public class Huffman() {
public Node BuildHuffmanCodeTree(String[] chars, int[] weights) {
for(int i = 0; i < chars.length; i++) {
Node tempNode = new Node(chars[i], weights[i]);
}
}
public static void main(String[] args) {
}
}
这是 Node 的代码,以防万一。Node 和 Huffman 都位于同一个文件夹中,所以 Huffman 应该能够看到 Node,对吧?
import java.util.Comparator;
public class Node{
Node child1;
Node child2;
Boolean isLeaf;
int weight;
String letter;
public Node(Node node1, Node node2) {
this.child1 = node1;
this.child2 = node2;
this.weight = node1.getWeight() + node2.getWeight();
this.letter = null;
this.isLeaf = false;
}
public Node(Node node1) {
this.child1 = node1;
this.child2 = null;
this.isLeaf = false;
this.weight = node1.getWeight();
}
public Node(String character, int num) {
this.child1 = null;
this.child2 = null;
this.isLeaf = true;
this.weight = num;
this.letter = character;
}
public int getWeight() {
return this.weight;
}
public String getChar() {
return this.letter;
}
}
解决方案
()
当您将其定义为时不需要类名,public class Name
并且您的方法public Node BuildHuffmanCodeTree(String[] chars, int[] weights)
不是类型,void
因此它需要返回语句。更改Huffman.java
为以下内容应该没有错误:
import java.util.PriorityQueue;
import java.util.Arrays;
public class Huffman {
public Node BuildHuffmanCodeTree(String[] chars, int[] weights) {
for(int i = 0; i < chars.length; i++) {
Node tempNode = new Node(chars[i], weights[i]);
}
return null; //Not clear about your wanted function, but return sentence is needed here
}
public static void main(String[] args) {
}
}
推荐阅读
- javascript - 我的字符串变量不会替换我的 if 语句中的数字
- macos - SwiftUI - 如何在 macOS 上隐藏窗口标题
- php - 错误:您的 SQL 语法有错误;无限自由
- azure-devops - 使用版本中库变量组中的变量从 Azure DevOps 设置 AWS 机密
- amazon-web-services - AWS Elasticsearch Service (Kibana) SAML 身份验证与 AWS Single Sign On
- windows - 用于从注册表项中删除所有权限的 Powershell 脚本
- gnuplot - gnuplot散点图,带颜色的标签
- python - Scipy odeint 没有参数或括号的方法
- python - Pandas 将二维数据结构化到 XYZ 表
- c++ - 从向量打印值时如何使用printf
?