Java:有关 int[] 方法的帮助...与哈夫曼树有关
我正在制定一种为霍夫曼树创建码字的方法。从中获取代码字的节点的符号被传递到该方法中。我不太确定如何解决这个问题,它必须返回一个 int[]。我编写了我认为可行的代码。如何正确使用 int[] 以便创建诸如 00101 之类的输出?谢谢
public int[] codeWordAsAry(int k) {
HuffTreeNode temp;
int[] codeWord;
int pos = 0;
temp = leaves[k];
while (temp.parentOf() != null){
if (temp.isLeftChild()){
codeWord[pos] = 1;
pos++;
}
else { //if isRightChild
codeWord[pos] = 0;
pos++;
}
}
return codeWord; }
,好的,所以我理解初始化大小,但现在我只是想知道是否可以使用这种方式打印出 01011 或其他组合的内容,就像我在 int[] 中正确增加位置的方式一样大批。那会打印出我要找的东西吗?
I am making a method to create the codewords for a huffman tree. The symbol of the node to get the codeword from is passed into the method. I'm not exactly positive how to go about this it has to return an int[]. I coded up what I thought might work. How do I properly use int[] so that I can create an output such as 00101? Thanks
public int[] codeWordAsAry(int k) {
HuffTreeNode temp;
int[] codeWord;
int pos = 0;
temp = leaves[k];
while (temp.parentOf() != null){
if (temp.isLeftChild()){
codeWord[pos] = 1;
pos++;
}
else { //if isRightChild
codeWord[pos] = 0;
pos++;
}
}
return codeWord; }
Ok so I understand the initializing the size but now I'm just wondering if its possible using this way to print out something along the lines of 01011 or other combinations like is the way im doing the increment of positions correct in the int[] array. will that print out what im looking for?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在尝试访问整数数组之前,您需要实例化它。
例如,
You need to instantiate the integer array before attempting to access it.
For example,