![](/img/trans.png)
[英]Why do I get a Runtime exception when I try to find values I just put in the hashmap?
[英]Why do I get this : [LMyStack.Node@addbf1 when I try to print right values of Stack?
這是我的Java代碼,用於創建堆棧並從堆棧彈出一個Node。 但是,當我將Nodes添加到Node[]
,我在打印到屏幕時獲得的值為: [LMyStack.Node; @ addbf1 。
我不知道是什么問題。 有什么幫助嗎?
public class MyStack{
private Node[] values;
public MyStack(){
}
public MyStack(Node[] values){
this.values=values;
}
//pop one Node from Stack
public Node pop(){
Node result=null;
if(values!=null && values.length-1>0){
result = values[values.length-1];
Node[] temp = new Node[values.length-1];
for(int i=0;i<values.length-1;i++){
temp[i]=values[i];
}
this.values=temp;
//Print the stack to screen
System.out.println("The stack is:"+this.value);
}
return result;
}
}
class Node {
private int value;
public Node(){
value=0;
}
public Node(int value){
this.value=value;
}
}
class Result{
public static void main(String []args){
Node node1 = new Node(1);
Node node2 = new Node(2);
Node node3 = new Node(3);
//Here is problem
Node[] n = {node1, node2, node3};
//System.out.println("Node is: "+n[1] );
MyStack stack = new MyStack(n);
stack.pop();
}
}
堆棧溢出中有很多這樣的示例。 請記住先搜索!
您將看到在Node對象數組上調用的默認toString()
方法。 [LMyStack.Node
表示節點實例的數組,而addbf1
是哈希addbf1
的十六進制表示。 使用循環:
for (Node node : values) {
System.out.println(node);
}
System.out.println(“堆棧為:” + this.value); 默認情況下將被視為System.out.println(“堆棧為:” + this.value.toString()); 在運行時返回classname @ hashcode
Object
默認toString()
實現調用它。
public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.