I created a simple list class. What I want to do is to create a method in SLList to give the size a SLList object. I want to do it recursively, however, the following size() method I created just does not work. I know other ways to realize it such as creating a helper method. But what I am curious about is that why does my size() does not work? The error message is the “size() is undefined for SLList.IntNode”. Why? Since I made the nested IntMode class just public and non-static, why it cannot use the method that is defined in SLList class?
public class SLList {
public class IntNode {
public int item;
public IntNode next;
public IntNode(int i, IntNode n) {
item = i;
next = n;
}
}
private IntNode first;
public SLList(int x) {
first = new IntNode(x, null);
}
public int size() {
if (first.next == null) {
return 1;
}
return 1 + first.next.size();
}
}
I am just new to Java, and quite confused about the private and static things, especially when it comes to the Class. Thank you for anyone answering me.
have you tried? In your code, how would it make sense to do so?
– Stultuske
2019-02-22 11:22:00Z
IntNode has no method called size(). And right now there is no recursion at all. You should google for singly linked list
– XtremeBaumer
2019-02-22 11:22:27Z
@Stultuske sorry, I made some indentation mistakes on the above code. Now I corrected it. but the key problem of mine is that can IntNode variable or object use the method defined in SLLlist?
– zangsy
2019-02-22 11:26:23Z
@XtremeBaumer Do you mean that the first.next actually needs to use the method defined in IntNode? Here the size() is not defined in IntNode, so it cannot use it, right? So, does that mean the nested Class cannot use the outer Class's method?
– zangsy
2019-02-22 11:31:38Z
@zangsy again: how would that make any sense?
– Stultuske
2019-02-22 11:32:23Z
You can fiddle it by adding an extra private method but it’s not particularly easy to reason about. I would avoid doing it this way unless absolutely necessary.
class SLList {
public class IntNode {
public int item;
public IntNode next;
public IntNode(int i, IntNode n) {
item = i;
next = n;
}
private int theSize()
{
return size();
}
}
private IntNode first;
public SLList(int x) {
first = new IntNode(x, null);
}
public int size() {
if (first.next == null) {
return 1;
}
return 1 + first.next.theSize();
}
}
Reason is : your method size() is in class SLList.
Hence it cannot be accessed by nested inner class IntNode.
size() is a method of SLList, not IntNode. You can refer to outer class method inside IntNode as follows:
public class SLList {
public class IntNode {
...
public int size() {
return SLList.this.size();
}
}
...
public static int size() {
...
}
}
Add a size method to IntNode class and access it from SLList size method to calculate the entire size of the list. The following code snippet is self explanatory. For more information about nested classes refer https://www.programiz.com/java-programming/nested-inner-class
public class SLList {
public class IntNode {
public int item;
public IntNode next;
public IntNode(int i, IntNode n) {
item = i;
next = n;
}
public int size() {
IntNode tmp = next;
if (tmp == null) {
return 1;
}
return 1 + tmp.size();
}
}
private IntNode first;
public SLList(int x) {
first = new IntNode(x, null);
}
public int size() {
if (first == null)
return 0;
return first.size();
}
public static void main(String[] args) {
SLList list = new SLList(10);
list.first.next = list.new IntNode(20, null);
list.first.next.next = list.new IntNode(30, null);
list.first.next.next.next = list.new IntNode(40, null);
System.out.println(list.size());
}
}