1

I use this codes to get all Folders:

 File MyDir[] = getExternalCacheDirs();

And then I use these code to show the path:

Toast.makeText(getApplicationContext(), MyDir[0].getPath(), Toast.LENGTH_LONG).show();
Toast.makeText(getApplicationContext(), MyDir[1].getPath(), Toast.LENGTH_LONG).show();

However, I wonder how to get the length of the file list, so I can use the for-loop to show all path

Like:

for (j = 0; j <= <size_of_the_list>; j += 1) { 
 Toast.makeText(getApplicationContext(),MyDir[j].getPath(),Toast.LENGTH_LONG).show();
}
Anoop M Maddasseri
  • 9,497
  • 3
  • 49
  • 68
Question-er XDD
  • 533
  • 3
  • 8
  • 24

5 Answers5

1

MyDir is an array, why dont you do:

// MyDir.length


for (j = 0; j < MyDir.length; j ++) { 
    Toast.makeText(getApplicationContext(), MyDir[j].getPath(),       Toast.LENGTH_LONG).show();
}

?

ΦXocę 웃 Пepeúpa ツ
  • 45,713
  • 17
  • 64
  • 91
1

You are searching for the length property

for (j=0; j< MyDir.length; j++) { 

}

Here is a more detail thread about it

Community
  • 1
  • 1
ThomasThiebaud
  • 10,285
  • 5
  • 48
  • 73
1

It here:

public static long folderSize(File directory) {
    long length = 0;
    for (File file : directory.listFiles()) {
        if (file.isFile())
            length += file.length();
        else
            length += folderSize(file);
    }
    return length;
}
mdtuyen
  • 4,210
  • 5
  • 25
  • 49
1

If you are unfamiliar with arrays, I would suggest that you check out the Arrays part of the nuts and bolts tutorial.

You are looking for the length property of the array:

for (j = 0; j < array.length; j += 1) { }

Note that you should be using < rather than <=, because there are array.length elements in the array, not array.length + 1. The last element of the array is array[array.length - 1] (assuming it is not empty).

Andy Turner
  • 131,952
  • 11
  • 151
  • 228
1

To answer your question, you can get the size of the array by calling .length on it.

array.length

You should also look at this to learn more about arrays. http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

Shiladittya Chakraborty
  • 4,138
  • 7
  • 42
  • 83