Reputation: 366
Sorry that I'm new in programming so my description might not correct. I designed a class that uses linkedNode structure:
class test {
public int data,
public test next;
public test(int data){this.data = data;}
}
and main:
public static void main(){
test t1 = new test(1), t2 = new test(2); //Line 1
t1.next = t2; //Line 2
t2.next = t1; //Line 3
t2 = null; //Line 4
Console.Write(t1.next.next.next.......data) // Line 5
}
So my next() seems can be indefinitely called, but I'm curious that how t2
is connected to t1.next
? if t2
is made a copy to t1.next
, then Line 3 will not work for making this as a loop; if t2
is only linked by address, then line 4 will break the loop. So I'm confused what happens behind my code?
Upvotes: 1
Views: 67
Reputation: 127603
D Stanley's answer is very good, but I wanted to give a more real world analogy with finer grained steps that might help you follow along. I will go through each part of your code and convert it in to this analogy. I changed the name of the parameter in the constructor to make things a tiny bit clearer.
class test {
You create manufacturing instructions called "test" for a what should be packed in a cardboard box and how to put things in it.
public int data,
There will be a slip of paper that will have the label "data" printed on it put in the box, this paper will have a spot to write in a number between -2,147,483,648 and 2,147,483,647. The paper has an already penciled in value of 0 as a default value.
public test next;
Another piece of paper labeled "next" will be put in the box, it is a special piece of paper that only lets you write in a the location of a shelf made to hold "test" boxes at the warehouse. This location will be left blank by default.
public test(int newData){this.data = newData;}
}
This is packing instructions, the instructions say "I will give you a slip of paper labeled "newData" with a number on it. You are to write on the slip of paper labeled "data" in the box the number from the slip "newData" I gave you. When you are done you throw away the slip of paper labeled "newData" that I originally gave to you."
public static void main() -->{<--
You go to work for the day.
test t1 = -->new test(1)<--
You go to your supplier, order a "test" box and hand them a slip of paper with a 1 on it. They ship this box off to the warehouse.
--> test t1 =<-- new test(1), t2 = new test(2); //Line 1
You get told that your box is sitting on shelf 0xAAAAAAAA, you print out that special piece of paper that only lets you write shelf numbers on it with the label "t1" on it, you write the value 0xAAAAAAAA on it.
test t1 = new test(1), -->t2 = new test(2);<-- //Line 1
You repeat the previous two steps but this time with a 2 given to the manufacture and a slip of paper with the label t2 gets 0xBBBBBBBB written on it.
-->t1.<--next = t2; //Line 2
Go to the wherehouse and find box the shelf that has the name that matches the number written on the paper labeled t1 (shelf 0xAAAAAAAA).
t1-->.next = t2<--; //Line 2
Open the box on that shelf, write on slip of paper that says "next" in the box the number that is on the "t2" paper you have (0xBBBBBBBB). Leave the "next" paper in the box and go back to the office with your "t2" paper.
-->t2.<--next = t1; //Line 3
Go to the wherehouse and find box the shelf that has the name that matches the number written on the paper labeled t2 (shelf 0xBBBBBBBB).
t2-->.next = t1<--; //Line 3
Open the box on that shelf, write on slip of paper that says "next" in the box the number that is on the "t1" paper you have (0xAAAAAAAA). Leave the "next" paper in the box and go back to the office with your "t1" paper.
t2 = null; //Line 4
Erase the number on your piece of paper labeled "t2"
Console.Write(-->t1.<--next.next.next.......data) // Line 5
Go to the wherehouse and find the shelf that has the number that is written on "t1" (0xAAAAAAAA) and find the box that is on it.
Console.Write(t1.-->next.<--next.next.......data) // Line 5
In the box you are currently at (0xAAAAAAAA) read the piece of paper labeled "next", then go find the shelf that has that number (0xBBBBBBBB)
Console.Write(t1.next-->.next.<--next.......data) // Line 5
In the box you are currently at (0xBBBBBBBB) read the piece of paper labeled "next", then go find the shelf that has that number (0xAAAAAAAA)
Console.Write(t1.next.next-->.next.<--......data) // Line 5
In the box you are currently at (0xAAAAAAAA) read the piece of paper labeled "next", then go find the shelf that has that number (0xBBBBBBBB)
Console.Write(t1.next.next.next-->.......<--data) // Line 5
Keep jumping back and forth between boxes on shelf 0xAAAAAAAA and 0xBBBBBBBB, each time reading the value of "next" and going to the shelf that the paper had written on it.
Console.Write(t1.next.next.next......-->.data<--) // Line 5
In the box you are currently at (?) read the piece of paper labeled "data"
-->Console.Write(t1.next.next.next.......data)<-- // Line 5
Give the number that was written on that "data" piece of paper to the "Console" company and tell them you want them to "Write" it.
}
You go home for the day.
You never modified the paper inside the boxes after they had their original values written on it, you only erased the number of the piece of paper you had in your office, so erasing that number has no effect on reading the "next" pieces of paper in the two boxes.
Here is a little bonus info about how objects get garbage collected using the same analogy.
t2 = null; //Line 4
--><---
Console.Write(t1.next.next.next.......data) // Line 5
}
You throw away your piece of paper with the label "t2" because you won't use "t2" for the rest of the work day.
t2 = null; //Line 4
Console.Write(t1.next.next.next......-->.data<--) // Line 5
--><---
}
You throw away the piece of paper labeled "t1" because you won't be using it for the rest of the work day.
public static void main() -->{
test t1 = new test(1), t2 = new test(2); //Line 1
t1.next = t2; //Line 2
t2.next = t1; //Line 3
t2 = null; //Line 4
Console.Write(t1.next.next.next.......data) // Line 5
}<--
At some time during the day, if management decides that the wherehouse is getting too full they go through everyone's office, they find all slips of paper that have warehouse shelves of any kind written on them and marked those shelves as "used", they then go to the boxes in those used shelves and find any other shelf numbers inside the box. They repeat this process till all the boxes that can be marked used are marked. They then throw away any boxes that were not marked used to free up space in the wherehouse.
Upvotes: 1
Reputation: 152624
Actually, neither of your conclusions are correct. A copy of the object is NOT made, but line 4 does not "break the loop". Let's break it down:
test t1 = new test(1), t2 = new test(2); //Line 1
This creates two "test" objects, and stores their references in local variables. Since they each have a value to identify them (that does not change in this example), we'll call them "Thing 1" and "Thing 2"
t1.next = t2; //Line 2
Fine. "Thing 1"'s next
property now references "Thing 2" (technically, the value of t2
- a reference to "Thing 2" - is copied to t1.next
)
t2.next = t1; //Line 3
Still good. "Thing 2"'s next
property now references "Thing 1" (technically, the value of t1
- a reference to "Thing 1" - is copied to t2.next
)
t2 = null; //Line 4
Fine. The variable t2
now points at nothing. The key misunderstanding here is that nothing has changed with "Thing 2". Only the variable that WAS referencing "Thing 2" has changed. "Thing 2" itself is unaffected. It still has a next
property that references "Thing 1". For that matter, "Thing 1" is unaffected as well. It has a next
property that references "Thing 2"
Console.Write(t1.next.next.next.......data) // Line 5
t1
references "Thing 1", whose next
property references "Thing 2", whose next
property references "Thing 1", whose next
property references "Thing 2", etc. So depending on how many times you call .next
, the result will be either 1
or 2
.
I'm curious that how t2 is connected to t1.next?
Up until line 4, they are referencing the same object. Line 4 then changes what t2
references (nothing) but does NOT change the value of t1.next
.
Upvotes: 4