gpt4 book ai didi

java - 如何按字母顺序对 Arraylist 进行排序? ( java )

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:38:48 26 4
gpt4 key购买 nike

<分区>

我是 Java 的新手,我对编码还不是很了解,但也许你可以帮助我解决我的问题。我有一个程序应该按正确的字母顺序列出所有歌曲。我想使用 Collections API。但是在我的代码中我没有做对。 Eclipse 没有给我一个错误,它在模拟器上运行,没有正确排序。也许你可以告诉我,将我必须做的事情转移到我的代码中,让它运行起来。抱歉没有你那么好 ;) ...但我还年轻,我想学习所有关于编码的知识。非常感谢,Vinzenz :)

public class SongsManager {

final String MEDIA_PATH = Environment.getExternalStorageDirectory()
.getPath() + "/";
private ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
private String mp3Pattern = ".mp3";
private String mp4Pattern = ".mp4";
private String MP3Pattern = ".MP3";
private String MP4Pattern = ".MP4";
private String m4aPattern = ".m4a";

// Constructor
public SongsManager() {

}

/**
* Function to read all mp3 files and store the details in
* ArrayList
* */
public ArrayList<HashMap<String, String>> getPlayList() {
System.out.println(MEDIA_PATH);
if (MEDIA_PATH != null) {
File home = new File(MEDIA_PATH);
File[] listFiles = home.listFiles();
if (listFiles != null && listFiles.length > 0) {
for (File file : listFiles) {
System.out.println(file.getAbsolutePath());
if (file.isDirectory()) {
scanDirectory(file);
} else {
addSongToList(file);
}
}
}

}
// return songs list array
return songsList;



}

private void scanDirectory(File directory) {
if (directory != null) {
File[] listFiles = directory.listFiles();
if (listFiles != null && listFiles.length > 0) {
for (File file : listFiles) {
if (file.isDirectory()) {
scanDirectory(file);
} else {
addSongToList(file);

}

}
}
}
}

private void addSongToList(File song) {
if (song.getName().endsWith(mp3Pattern) || song.getName().endsWith(mp4Pattern) || song.getName().endsWith(MP4Pattern) || song.getName().endsWith(MP3Pattern) || song.getName().endsWith(m4aPattern)) {
HashMap<String, String> songMap = new HashMap<String, String>();
songMap.put("songTitle",
song.getName().substring(0, (song.getName().length() - 4)));
songMap.put("songPath", song.getPath());

// Adding each song to SongList
songsList.add(songMap);
}
}

public static void main (String[] args) {
List<String> songsList = new LinkedList <String>();
System.out.println(songsList);
Collections.sort(songsList);
System.out.println(songsList);

}



}

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com