I have created a linkedlist which stores two elements. Can someone tell me how to access the second element. In this case, the lastName
This is what I came up with so far.
public class Bank
{
private LinkedList<List> words = new LinkedList<List>();
public void startup()
{
words.add(new List("Fred","Cool"));
}
This is my list class
public class List
{
public List(String name, String lastName)
{
this.name = name;
this.lastName = lastName;
}
You have a number of problems in your code:
I suggest you rename your
Listclass toPerson, as object of that type clearly does not represent lists.nameandlastNamewill not be elements of the list. They are arguments to the constructor of theList(readPerson) class.You have no fields in your
List, so you can’t doTo access the second element in the
wordslist, you doHowever, since you’ve only added one element you won’t be able to use the above expression.
Here’s a complete example to get you on the right track: