I'm working on a linked list code and wanted to access head.next.next.data. Is there a means where I can store .next.next.data to a variable x so that I can make the call head.x and retrieve the result provided I have data populated in the list? I want to implement
public class CustomSOL<E> implements SOL<E>
{
private static class customNode<E>
{
private customNode<E> next;
private E data;
public customNode(E dataItem)
{
this.data = dataItem;
this.next = null;
}
private customNode(E dataItem, customNode<E> nodeRef)
{
this.data = dataItem;
this.next = nodeRef;
}
}
private customNode<E> head;
private int size;
public CustomSOL()
{
head = null;
size = 0;
}
public void solLookup(int amount)
{
String s = "next";
StringBuilder sb = new StringBuilder(14);
for(int i = 0; i < amount; i++)
sb.append(".").append(s);
System.out.println(head.s.data);
}
}
so in main if I have
CustomSOL<String> list = new CustomSOL<String>();
list.solAdd("Apple");
list.solAdd("Orange");
list.solAdd("Banana");
list.solAdd("Strawberry");
list.solAdd("Papaya");
and I call list.solLookup(3):
String s = "next.next.next";
System.out.println(head.s.data);
I want to see Strawberry printed out, which it obviously won't because s is string. But is there a way, just like how javascript and PHP have variable functions, to replace all those next calls with a variable s? I'm using a string because I can use the amount parameter in solLookup to create the appropriate next call notations. Thank you!