어떻게 링크 된 목록 노드를 만들고 일부 데이터를 추가 한 다음 다른 방법으로 헤드를 이동해도 헤드가 여전히 호출 수신자 방법으로 유지됩니까? 예를 들어링크 된 목록 포인터 범위
:
public static void append(Node n, int d) {
while (n.next != null) {
n = n.next;
}
n.next = new Node(d);
}
public static void test(Node n) {
n = n.next;
}
public static void main(String[] args) {
Node head = new Node(5);
append(head, 4);
append(head, 3);
test(head); //this moves the head to head.next
//why is head still = 5 after we get here?
}
'test' 메소드는 ** 아무 것도 ** 수행하지 않습니다. –
머리를 참고로 통과해야합니까? 테스트에서, n = n.next, n.data = 4를 그 메소드에서 움직였을 때, main에서 어떻게 온다? head.data = 5? – MiketheViking90
@ MiketheViking90 "머리를 참고로 전달해야합니까?" 아니요, 참조는 값으로 전달됩니다. – Boann