开发者

ArrayList is returning duplicate values instead of listing out all values

When I run my code its meant to display the playCount of each AudioFile in descending order. it can manage this if all of the playCounts are unique however if two playCounts are the same it will just return the same AudioFile twice instead of the two seperate AudioFiles

I am still quite new so i apologize if the mistake is very obvious or i haven't explained properly

This is for a HomeWork Assignment so for ArrayList i am only limited to get, add, size and remove methods as well as i can`t include any external libraries other than Scanner and ArrayList

This is my code which relates to the issue

Top 10 Method

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) {
                disp开发者_高级运维lay(playcount);
            } else if (playcount.length >= 10) {
                display(playcount, 10);
            }
        }
        return null;
    }

Display for less than 10 AudioFiles

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");
        }
    }

Display for 10 or more AudioFiles

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");
        }
    }

Sorting playCount in desc order

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);
    }

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;
    }
    
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜