I am new to programming and get stuck with an access issue. Currently I try to exercise LinkedList - by creating one by myself and later use it.
I have 3 classes in the same directory - custom 'MyLinkedList', another custom one, that I called 'Month' and 'Main'. My idea is to creat 12 Objects of Month class, as each has 3 fields and add them to a LinkedList - I did it. Now I have problem, when I want to retrieve a certain value of a certain Oject's field. I get the address of the Object, but can't figure out, how to reach the value behind this address.
Example - I create a MyLinkedList with 12 Nodes and put an Object of type Month in every Node. Later I want to let's say get the field called 'season' of March(Object of type Month), which is stored in my Linked List.
What I do - access the field called "item" of my LinkedList, which field contains an Object(address of the Object) of type Month. Now I can't figure out, how can I proceed further, to reach the Object behind this address and retrieve the value, which the field of this Object keeps.
What I understand is - 1) I have a reference variable called 'myList' in the Stack, which contains address of Object of 'MyLinkedList' type in the Heap. 2) The place in the memory, corresponding to this adrress holds another 12 adresses of the 12 'Nodes'. 3) Each Node(piece of memory), holds the address of an Object 'Month'. 4) The place in the memory corresponding to Month's address holds the addresses of the 3 fields(of the Object). 5) Behind this last addresses(of the fields) there are placed the desired values. So I can only reach step 3, but cannot continue further. Would you advise how to proceed furhter.
I put some code of the 3 methods:
this is my code:
public class MyLinkedList {
private class Node{
Object item;
Node next;
Node(Object item){
this.item = item;
this.next = null;
}
Node(Object item, Node next){
this.item = item;
this.next = next;
}
}
private Node head;
private Node tail;
private int count;
public void addNode(Object element){
Node newNode = new Node(element);
if(head == null){
head = newNode;
head.next = tail;
tail = head;
}
else {
tail.next = newNode;
tail = newNode;
}
count++;
}
public void printList(){
Node currentNode = head;
while(currentNode != null){
if(currentNode.next != null){
System.out.print(currentNode.item + ", ");
}
else {
System.out.println(currentNode.item + ";");
}
currentNode = currentNode.next;
}
}
}
public class Month {
private String season;
private int length;
public int index;
public Month(int index){
this.index = index;
}
public String getSeason(){
return season;
}
public void setSeason(String season){
this.season = season;
}
public int getLength(){
return length;
}
public void setLength(int length){
this.length = length;
}
}
public class Main {
public static void main(String[] args) {
Month January = new Month(0);
Month February = new Month(1);
Month March = new Month(2);
/*
....more of the same here
*/
January.setLength(31);
February.setLength(28);
March.setLength(31);
/*
....more of the same here
*/
January.setSeason("Winter");
February.setSeason("Winter");
March.setSeason("Spring");
/*
....more of the same here
*/
MyLinkedList myList = new MyLinkedList();
myList.addNode(January);
myList.addNode(February);
myList.addNode(March);
/*
....more of the same here
*/
System.out.println("Initially created list:");
myList.printList();
System.out.println();
}