i understand what the code does. but i dont understand a few lines:
package il.co;
public class LinkedListEx2 {
public static void main(String[] args) {
LinkedList list=new LinkedList();
list.add(new Car("BMW", 150000));
list.add(new Car("SUBARU", 60000));
list.add(new Car("TOYOTA", 58000));
list.print();
}
}
class LinkedList{
private Unit head;
public void add (Car data){
Unit newUnit =new Unit(data);
if(head==null)
head=newUnit;
else{
Unit current=head;
while (current.next!=null)// current.next, doesnt current and next refer to the same object/class? isnt current.next = null from the begining. whats the value of current.next?
current=current.next;// why is that line importnant . why to swap current next
current.next=newUnit;
}
}
public void print() {
print(head);
}
private void print(Unit unit){
if(unit==null)
return;
System.out.println(unit.info);
print(unit.next);
}
}
class Unit{
Car info;
Unit next;
public Unit(Car info) {
this.info=info;
}
}
class Car {
String model;
int price;
public Car(String model, int price) {
this.model = model;
this.price = price;
}
@Override
public String toString() {
return "Car [model=" + model + ", price=" + price + "]";
}
}