Highlighted
Absent Member.
Absent Member.
1751 views

Getting the length of an array of objects

Jump to solution

Trying to use a call to the File.listRoots method in java which returns a list of the root file systems (Windows drives in my case) available to me as an array of File objects.  I'm trying to figure out how to get the number of root file systems returned so I can iterate through and can't seem to get it.  Any help would be appreciated.

Sample Code

01 class1 type performance.
01 diskdrive type File.
01 rootpath string.
01 writeable type boolean.
01 drives type File occurs any.
01 i binary-long.
01 freespace type long.
procedure division using by value args as String occurs any.
set class1 to new performance.
set drives to type File::listRoots().
perform varying i from 1 by 1 until i > 4
set rootpath to drives(i)
set diskdrive to new File(rootpath)
set writeable to diskdrive::canWrite()
set freespace to diskdrive::getFreeSpace()
display "Free Space = " freespace
end-perform.

Java - essentially I want the value in roots.length

        // Determine all the root paths for the system.
        File[] roots = File.listRoots();

        System.out.println("The system contains root paths:");
        for (int i = 0; i < roots.length; i++)
        {
            System.out.println("\t" + roots.getAbsolutePath());
        }
    }
Tags (1)
0 Likes
2 Solutions

Accepted Solutions
Highlighted
Micro Focus Expert
Micro Focus Expert

RE: Getting the length of an array of objects

Jump to solution

You could use a perform varying on the array returned from File::listRoots() for example:

   procedure division using by value args as String occurs any.
           perform varying
                diskdrive as type File through type File::listRoots()
              display diskdrive " -> Free Space = "
                   diskdrive::getFreeSpace() " "
                   " writable? " diskdrive::canWrite()
           end-perform.

Or you can use "size of" eg:

     declare f as type File occurs any

     set f to type File::listRoots()
     display size of f

View solution in original post

0 Likes
Highlighted
Absent Member.
Absent Member.

RE: Getting the length of an array of objects

Jump to solution

apparently length of args does what I want

View solution in original post

0 Likes
3 Replies
Highlighted
Micro Focus Expert
Micro Focus Expert

RE: Getting the length of an array of objects

Jump to solution

You could use a perform varying on the array returned from File::listRoots() for example:

   procedure division using by value args as String occurs any.
           perform varying
                diskdrive as type File through type File::listRoots()
              display diskdrive " -> Free Space = "
                   diskdrive::getFreeSpace() " "
                   " writable? " diskdrive::canWrite()
           end-perform.

Or you can use "size of" eg:

     declare f as type File occurs any

     set f to type File::listRoots()
     display size of f

View solution in original post

0 Likes
Highlighted
Absent Member.
Absent Member.

RE: Getting the length of an array of objects

Jump to solution

apparently length of args does what I want

View solution in original post

0 Likes
Highlighted
Absent Member.
Absent Member.

RE: Getting the length of an array of objects

Jump to solution

Thanks Steve

0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.