So I'm simulating a File system for my Operating Systems class. I'm kinda lazy and didn't want to do it in C, so I did it in Java :(. I also didn't make any new data structures, I just made a File and Directory class, where the Directory class has an attribute called contents, which is an ArrayList<Object> that contains File and Directory objects.

In the client program, however when I call getContents() on a particular directory, I get an ArrayList<Object>, and when I take the objects out, I can't perform any of the File or Directory methods, even if I cast them to that particular data type.

Why can't I do this? Is there any way to do it where you can have one ArrayList holding both objects that you can manipulate? That seems alot easier and more elegant than the workaround I decided to do, where a directory has two ArrayLists. One is an ArrayList<File> files, and the other is ArrayList<Directory> directories.

Thanks

There isn't a "Directory" class. Both files and directories are represented by the File class, so all you need is is an ArrayList<File>

I don't understand. I made two classes, File and Directory. I know that most Operating systems treat a directory as a file anyway, but I didn't do it that way. Even so, when taking the Directory out of the ArrayList<File>, wouldn't I still encounter the same downcasting problem?

I hadn't realised that you created your own classes - the standard File class covers both files and directories and works well enough for most people.

You can cast down to your classes from Object provided you take precauions to ensure your cast is valid. eg

for (Object o : myArrayListOfObjects) {
   if (o instanceof File) {
      File f = (File) o;
      // do File stuff with f
   } else (if o instanceof Directory) {
      // etc

To keep your classes you could make them subclasses of a common abstract superclass (I'm sure there are lots of attributes and methods that they could inherit). Make that the type of the ArrayList. Now you can call the inherited or overridden methods directly, and cast to File or Directory when necessary

I don't understand. I made two classes, File and Directory. I know that most Operating systems treat a directory as a file anyway, but I didn't do it that way. Even so, when taking the Directory out of the ArrayList<File>, wouldn't I still encounter the same downcasting problem?

In Java directories and files are treated as the same object. so, you won't encounter any down casting issues.

Thanks. I did something like that, only instead of making a File object in the body of the loop, I did something like this:

for(int i = 0; i < contents.size(); i++)
{
   Object currentEntry = contents.get(i);
   if(i instanceof File)
   currentEntry = (File)currentEntry;
}

It makes sense now why my implementation didn't work. I thought java would let me do the subclass methods if the "right side" had been casted into the data type I wanted. I'll use that now.

currentEntry = (File)currentEntry;
Yeah, currentEntry is declared as being a reference to an Object, so the compiler will only allow you to use it for methods that Object has.
(File)currentEntry;
correctly converts the reference to be a reference to a File, but you assign that to an Object reference, which is OK because a File is an Object, but that's all you've got - a reference to an Object again.
That's why you need a variable that's declared as being a reference to a File.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.