You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ArrayList返回重复值而非全部值:播放量排序展示异常求助

问题:相同播放量的音频文件重复返回,无法正确展示

我的代码原本要按播放量(playCount)降序展示所有AudioFile,当所有播放量唯一时能正常运行,但如果有两个相同播放量的文件,会重复返回同一个AudioFile,而不是两个不同的文件。

这是作业任务,限制只能使用ArrayList的get、add、size和remove方法,不能使用Scanner和ArrayList之外的外部库。


相关代码

Top 10 方法

public String topTen() {
    int[] playcount = new int[audioData.size()];
    if (audioData.size() <= 0) {
        System.out.println("\nNothing here to display\n");
        return null;
    }
    for (int index = 0; index < audioData.size(); index++) {
        playcount[index] = audioData.get(index).getPlayCount();
    }
    bubbleSort(playcount, false);
    if (audioData != null) {
        System.out.println("\nThese are your top 10 tracks\n");
        if (playcount.length < 10) {
            display(playcount);
        } else if (playcount.length >= 10) {
            display(playcount, 10);
        }
    }
    return null;
}

少于10个AudioFile的display方法

private void display(int data[]) {
    if (data != null && data.length > 0) {
        for (int index = 0; index < audioData.size(); index++) {
            AudioFile audio = searchPlay(audioData.get(index).getPlayCount());
            System.out.println("Code: " + audio.getCode() + ", " + "Title: " + audio.getTitle() + ", " + "Plays: "
                    + audio.getPlayCount() + "\n");
        }
    } else {
        System.out.println("No data to display.\n");
    }
}

10个及以上AudioFile的display方法

private void display(int data[], int count) {
    if (data != null && data.length > 0 && data.length >= count && count > 0) {
        for (int index = 0; index < count; index++) {
            AudioFile audio = searchPlay(data[index]);
            System.out.println("Code: " + audio.getCode() + ", " + "Title: " + audio.getTitle() + ", " + "Plays: "
                    + audio.getPlayCount());
        }
    } else {
        System.out.println("No data to display.\n");
    }
}

playCount降序排序方法

private void bubbleSort(int[] data, boolean ascending) {
    int swaps;
    do {
        swaps = 0;
        for (int index = 0; index < data.length - 1; index++) {
            if (data[index] > data[index + 1] && ascending || data[index] < data[index + 1] && !ascending) {
                int temp = data[index];
                data[index] = data[index + 1];
                data[index + 1] = temp;
                swaps++;
            }
        }
    } while (swaps > 0);
}

searchPlay方法

private AudioFile searchPlay(int id) {
    AudioFile target = null;
    for (int index = 0; index < audioData.size(); index++) {
        AudioFile audio = audioData.get(index);
        if (audio.getPlayCount() == id) {
            target = audio;
            break;
        }
    }
    return target;
}

问题根源

  1. searchPlay方法的缺陷:当多个AudioFile有相同playCount时,该方法找到第一个匹配项后立刻break终止循环,只会返回第一个匹配的文件,后续相同播放量的文件被完全忽略。
  2. 排序逻辑的脱节:单独对playCount数组排序,丢失了播放量与原AudioFile对象的对应关系——排序后的播放量数组无法关联到所有对应的文件,只能通过searchPlay去查找,而searchPlay本身又有缺陷。

解决方案(符合作业限制)

直接对audioData列表中的AudioFile对象按playCount降序排序,这样排序后可以直接按顺序取前10个(或全部)展示,无需单独处理播放量数组和搜索逻辑。

修改后的代码

1. 针对AudioFile列表的冒泡排序(仅用ArrayList允许的方法)

// 按playCount降序排序AudioFile列表,仅使用get、add、size、remove方法
private void bubbleSortAudioFiles() {
    int swaps;
    do {
        swaps = 0;
        for (int index = 0; index < audioData.size() - 1; index++) {
            AudioFile current = audioData.get(index);
            AudioFile next = audioData.get(index + 1);
            // 降序规则:当前播放量小于下一个时交换位置
            if (current.getPlayCount() < next.getPlayCount()) {
                // 用remove和add实现元素交换(替代set方法)
                audioData.remove(index);
                audioData.add(index, next);
                audioData.remove(index + 1);
                audioData.add(index + 1, current);
                swaps++;
            }
        }
    } while (swaps > 0);
}

2. 简化后的Top 10方法

public String topTen() {
    if (audioData.size() <= 0) {
        System.out.println("\nNothing here to display\n");
        return null;
    }
    // 直接对AudioFile列表降序排序
    bubbleSortAudioFiles();
    System.out.println("\nThese are your top 10 tracks\n");
    // 确定展示数量:最多10个,不足10则展示全部
    int displayCount = Math.min(audioData.size(), 10);
    for (int index = 0; index < displayCount; index++) {
        AudioFile audio = audioData.get(index);
        System.out.println("Code: " + audio.getCode() + ", Title: " + audio.getTitle() + ", Plays: " + audio.getPlayCount());
    }
    return null;
}

改动说明

  • 移除了冗余的playCount数组、display方法和searchPlay方法,逻辑更简洁
  • 直接对AudioFile对象排序,保留所有文件的关联信息,相同播放量的文件会被正确保留并展示
  • 完全使用ArrayList允许的get、add、size、remove方法,符合作业限制

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.09 03:25:30