android_su
android_su

Reputation: 1687

Is File.list() ordered(java)

I make a simple test

I have some files in the dir A, and i call file.list() several times ,then print the result

I find that the result always has the same sequence

I want to know what decide the file.list()'s order?

I don't want to sort the result, so do not tell me to use Comparator.

I just want to know what decide the file.list()'s order?

And I notice that the javadoc tell us:

There is no guarantee that the name strings in the resulting array will appear in any specific order; they are not, in particular, guaranteed to appear in alphabetical order.

So why do I get the same ordered result everytime?

Any iedas?

Thanks

ps
OS:Ubuntu 10.04
android4.03

Let's make it easier
choose a os:Ubuntu 10.04
and choose java 1.6
We only consider one situation, and now , why still I get the same result everytime?

Thanks for everyone!

Upvotes: 0

Views: 231

Answers (3)

vishal_aim
vishal_aim

Reputation: 7854

It depends on atleast OS on which it is executed. I've observed earlier myself that on Windows it was returning in alphabetical order and on Linux it was the reverse order. Never rely on it as it is mentioned in the doc.

Upvotes: 0

Aleksander Gralak
Aleksander Gralak

Reputation: 1509

I think that it is OS dependent or file system dependent. Java query file system for the list of files. The result therefore is unknown for JAVA.

So to make sure that the list is in desired order you should sort it. However if you use quick sort then it should be fast anyway. Usually list will be sorted by OS already.

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1501163

So why do I get the same ordered result everytime?

Because "not guaranteed to do X" isn't the same as "guaranteed not to do X". It so happens that on your system, the implementation may always return the results in alphabetical order. It may depend on the file system in use. It almost certainly depends on the operating system. It may depend on the verison of Java you're using.

The important thing is that it's not guaranteed, so you shouldn't rely on it.

Upvotes: 3

Related Questions