I need to display a static folder hierarchy in java. I already have a folder class defined and have to use the same. No use of File class is allowed! Class folder is:
class Folder
{
boolean isFile;
String folderName;
List<Folder> subFolders;
}
The code i am trying to use is :
class LisF
{
public static void main(String args[])
{
Folder a=new Folder("C:/hello");
Folder b=new Folder("C:/one");
Folder c=new Folder("C:/two");
a.subFolders.add(b);
b.subFolders.add(c);
Iterator itr = a.subFolders.iterator();
Iterator itr1= b.subFolders.iterator();
while(itr.hasNext()) {
Object element = itr.next();
//System.out.print("The list is "+(Folder)element.putFName());
System.out.println("The lis is "+element);
}
}
}
It runs and gives the output as Folder@1234 which no doubt gives the name of the object. But i need the name of the string passed i.e to display the subfolders of hello it should display one and subFolders of one should display two! But instead i am getting the object names!!!!
You should implement the
toStringmethod in the classes you want/need to print the objects asStrings. In this case, you need to implement it in theFolderclass: