You need to enable JavaScript to run this app. Java实现SpotifyPlaylist播放列表类:已有Song类如何编写数组逻辑
优惠活动
大模型
产品
解决方案
定价
更多

Java实现SpotifyPlaylist播放列表类:已有Song类如何编写数组逻辑

Spotify播放列表Java项目实现指导

一、SpotifyPlaylist类完整实现

首先你需要先补全Song类的所有getter方法,否则以下代码会编译报错。

import java.util.Arrays;
import java.util.Comparator;

public class SpotifyPlaylist {
    // 定义播放列表最大容量
    private static final int MAX_CAPACITY = 1000;
    // 存储歌曲的数组
    private Song[] songs;
    // 记录当前播放列表中实际存储的歌曲数量
    private int currentSize;

    /**
     * 构造方法:初始化数组和计数器
     */
    public SpotifyPlaylist() {
        songs = new Song[MAX_CAPACITY];
        currentSize = 0;
    }

    /**
     * 添加歌曲功能
     * @param song 要添加的歌曲对象
     * @return 添加成功返回true,列表已满返回false
     */
    public boolean addSong(Song song) {
        if (currentSize >= MAX_CAPACITY) {
            System.out.println("播放列表已满,无法添加新歌曲");
            return false;
        }
        songs[currentSize] = song;
        currentSize++;
        return true;
    }

    /**
     * 打印所有歌曲
     */
    public void printAllSongs() {
        System.out.println("===== 播放列表全部歌曲 =====");
        for (int i = 0; i < currentSize; i++) {
            System.out.println(songs[i]);
        }
        System.out.println("===========================");
    }

    /**
     * 打印指定专辑下的全部歌曲
     * @param album 要筛选的专辑名称
     */
    public void printSongsByAlbum(String album) {
        System.out.println("===== 专辑《" + album + "》的歌曲 =====");
        for (int i = 0; i < currentSize; i++) {
            if (album.equals(songs[i].getAlbum())) {
                System.out.println(songs[i]);
            }
        }
        System.out.println("================================");
    }

    /**
     * 打印指定流派的全部歌曲
     * @param genre 要筛选的流派
     */
    public void printSongsByGenre(String genre) {
        System.out.println("===== 流派为" + genre + "的歌曲 =====");
        for (int i = 0; i < currentSize; i++) {
            if (genre.equals(songs[i].getGenre())) {
                System.out.println(songs[i]);
            }
        }
        System.out.println("================================");
    }

    /**
     * 打印指定艺术家的全部歌曲
     * @param artist 要筛选的艺术家名称
     */
    public void printSongsByArtist(String artist) {
        System.out.println("===== 艺术家" + artist + "的歌曲 =====");
        for (int i = 0; i < currentSize; i++) {
            if (artist.equals(songs[i].getArtist())) {
                System.out.println(songs[i]);
            }
        }
        System.out.println("================================");
    }

    /**
     * 打印播放量Top10热门歌曲
     */
    public void printTop10Songs() {
        System.out.println("===== 播放量Top10歌曲 =====");
        // 复制当前有效歌曲到临时数组,避免打乱原列表顺序
        Song[] tempArr = Arrays.copyOf(songs, currentSize);
        // 按播放次数降序排序
        Arrays.sort(tempArr, new Comparator<Song>() {
            @Override
            public int compare(Song o1, Song o2) {
                return o2.getNumOfTimesPlayed() - o1.getNumOfTimesPlayed();
            }
        });
        // 不足10首则取全部
        int printNum = Math.min(10, tempArr.length);
        for (int i = 0; i < printNum; i++) {
            System.out.println((i+1) + ". " + tempArr[i]);
        }
        System.out.println("==========================");
    }

    /**
     * 播放指定标题的歌曲,播放次数+1
     * @param title 要播放的歌曲标题
     * @return 播放成功返回true,未找到歌曲返回false
     */
    public boolean playSong(String title) {
        for (int i = 0; i < currentSize; i++) {
            if (title.equals(songs[i].getTitle())) {
                songs[i].playAgain();
                System.out.println("正在播放:" + title);
                return true;
            }
        }
        System.out.println("未找到歌曲:" + title);
        return false;
    }

    /**
     * 根据歌曲标题删除指定歌曲
     * @param title 要删除的歌曲标题
     * @return 删除成功返回true,未找到歌曲返回false
     */
    public boolean deleteSongByTitle(String title) {
        for (int i = 0; i < currentSize; i++) {
            if (title.equals(songs[i].getTitle())) {
                // 将删除位置后面的所有元素前移一位
                for (int j = i; j < currentSize - 1; j++) {
                    songs[j] = songs[j+1];
                }
                // 最后一个位置置空,方便垃圾回收
                songs[currentSize - 1] = null;
                currentSize--;
                System.out.println("已删除歌曲:" + title);
                return true;
            }
        }
        System.out.println("未找到要删除的歌曲:" + title);
        return false;
    }

    /**
     * 辅助方法:判断歌曲是否在播放列表中
     * @param song 要判断的歌曲对象
     * @return 存在返回true,不存在返回false
     */
    public boolean isSongExist(Song song) {
        for (int i = 0; i < currentSize; i++) {
            if (song.equals(songs[i])) {
                return true;
            }
        }
        return false;
    }
}

二、测试类实现(演示所有功能)

public class PlaylistTest {
    public static void main(String[] args) {
        // 创建播放列表实例
        SpotifyPlaylist playlist = new SpotifyPlaylist();

        // 准备测试歌曲数据
        Song s1 = new Song("夜曲", "周杰伦", "十一月的萧邦", 233, "流行", 1200);
        Song s2 = new Song("晴天", "周杰伦", "叶惠美", 268, "流行", 3200);
        Song s3 = new Song("以父之名", "周杰伦", "叶惠美", 321, "流行", 2800);
        Song s4 = new Song("加州旅馆", "老鹰乐队", "Hotel California", 391, "摇滚", 5600);
        Song s5 = new Song("波西米亚狂想曲", "皇后乐队", "A Night at the Opera", 354, "摇滚", 7200);

        // 测试添加歌曲
        playlist.addSong(s1);
        playlist.addSong(s2);
        playlist.addSong(s3);
        playlist.addSong(s4);
        playlist.addSong(s5);

        // 测试打印所有歌曲
        playlist.printAllSongs();

        // 测试按专辑打印
        playlist.printSongsByAlbum("叶惠美");

        // 测试按流派打印
        playlist.printSongsByGenre("摇滚");

        // 测试按艺术家打印
        playlist.printSongsByArtist("周杰伦");

        // 测试播放歌曲
        playlist.playSong("晴天");
        System.out.println("播放后《晴天》的播放次数:" + s2.getNumOfTimesPlayed());

        // 测试打印Top10歌曲
        playlist.printTop10Songs();

        // 测试删除歌曲
        playlist.deleteSongByTitle("夜曲");
        System.out.println("删除《夜曲》后的播放列表:");
        playlist.printAllSongs();
    }
}

三、注意事项

  • 所有字符串匹配都使用equals方法,不要用==,避免匹配失败
  • 数组遍历范围控制在0到currentSize-1之间,不要遍历整个1000长度的数组,避免空指针异常
  • Song类的所有私有属性的getter方法必须全部补全,否则代码无法编译

内容的提问来源于stack exchange,提问作者javaStudent222222

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.15 09:36:04