我正在解決DFS問題,所以我使用了ArrayList的數組。這裏是我的代碼如何在java中打印arrayList作爲字符串
ArrayList<Node> graph[] = new ArrayList[N+1];
int u, v, w;
for (int i = 1; i <=N; i++){
graph[i] = new ArrayList<Node>();
}
for(int i=0;i<N-1;i++){
u=sc.nextInt();
v=sc.nextInt();
w=sc.nextInt();
graph[u].add(new Node(v,w));
graph[v].add(new Node(u,w));
}
System.out.println(graph[1].get(0));----------(1)
對於上面打印語句,我得到的輸出[email protected]
。我不知道爲什麼我收到了這一點。 我輸入:
1
3
1 2
1 3
2 3
plz幫助我如何從ArrayList中
編輯的陣列打印精確的輸出:Node類:
class Node {
static int i;
int distance;
Node(int i, int distance) {
this.i = i;
this.distance = distance;
}
}
Object類的默認繼承方法'toString()'用於將節點轉換爲String。重寫Node類中的'toString()'方法。 – GurV
@GurwinderSingh:我感覺後續問題來了... –
Thanku @GurwinderSingh但仍然我得到了相同的輸出[節點@ 1db9742] –