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
  • 10,213
  • 3
  • 52
  • 73
Question-er XDD
  • 640
  • 3
  • 12
  • 27

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 ツ
  • 47,427
  • 17
  • 69
  • 97
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
  • 11,331
  • 6
  • 54
  • 77
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,470
  • 5
  • 28
  • 50
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
  • 137,514
  • 11
  • 162
  • 243
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,270
  • 8
  • 45
  • 94