Im trying to list the items in an array list from another class.
I have 3 classes in total
Tell – The main program
Item – The class that creates individual telephone directory items
Directory – A directory object which stores all the items.
What I’m trying to do is list all the items within the array list created in “directory” and im doing it like this….
TELL
if (Directory.entries.size>0)
{
for (int i=0; i<Directory.entries.size(); i++) // Check size of array
{
Directory.entries.get(i).showRecord();
}
}
DIRECTORY
public void printItem()
{
//I HAVE NO IDEA WHAT GOES HERE
}
ENTRY
public class Entry
{
String name;
String telNo;
public Entry(String aName, String aTelNo )
{
setNumber(aTelNo);
setName(aName);
}
// Add methods here
private void setNumber(String aTelNo)
{
telNo = aTelNo;
}
public String getNumber()
{
return telNo;
}
private void setName(String aName)
{
name = aName;
}
public String getName()
{
return name;
}
public String toString()
{
return name + "." + telNo;
}
}
However I havnt got the foggiest what needs to go in the printItem() Method to allow me to print the individual items in the directory.
Could someone point me in the right direction? This is the first time I have ever really used array lists accross multiple classes and I’m very confused.
Since you have overrided the
toStringin yourEntryclass, so what all you need to do is iterate over your List and print each item. It will automatically invoke the toString of your Entry class and print the returned value: –Put this code in your
printmethod, which is inDirectoryclass.entriesis thestaticlist you have in yourDirectoryclass.And from your
Tellclass, just invoke yourprintmethod:-