2014-01-29 20 views
-1

此問題已在此處提出。它只是參考了Finding the 3 most recently modified files in a long list of files提供的解決方案之一。關於「按照java高效方式進行最後修改的文件排序」的進一步說明

我試着給解決方案添加評論,但我沒有足夠的聲望點評論,所以我在這裏問。該解決方案提供了一種通過上次修改來排序文件的方法。

public static void sortFilesDesc(File[] files) 
{   
    File firstMostRecent = null; 
    File secondMostRecent = null; 
    File thirdMostRecent = null; 
    for (File file : files) { 
     if ((firstMostRecent == null) 
       || (firstMostRecent.lastModified() < file.lastModified())) { 
      thirdMostRecent = secondMostRecent; 
      secondMostRecent = firstMostRecent;    
      firstMostRecent = file; 
     } else if ((secondMostRecent == null) 
       || (secondMostRecent.lastModified() < file.lastModified())) { 
      thirdMostRecent = secondMostRecent; 
      secondMostRecent = file; 
     } else if ((thirdMostRecent == null) 
       || (thirdMostRecent.lastModified() < file.lastModified())) { 
      thirdMostRecent = file; 
     } 
    } 
} 

該方法將採用文件數組作爲參數,並假設它們根據最後修改的進行排序。 從這個方法我不明白,它是如何修改輸入數組,使其中的所有元素都排序。在代碼中,我們只改變局部變量fi​​rstMostRecent,secondMostRecent和thirdMostRecent的值。數組如何被修改?根據我的理解,可能會有些東西缺失,但我沒有得到它。請澄清我的困惑。

回答

0

輸入數組未被修改。 這是查找第一個,第二個&第三個最後修改文件的代碼。而已。

執行該for循環後,您可以獲得前3個最後修改的文件。

+0

哦好的。沒有人在那篇文章中提到過這個。問題是對大量文件進行排序。我預計它會以某種方式整理整個陣列。感謝@vengets – user3244615

相關問題