找到索引,替换值并将所需索引向下移动一个

时间:2016-02-11 21:44:10

标签: java arrays

大家好,这是我第一次在这个网站上写一个问题,虽然我总是来这里寻求有关编程任务的帮助。所以请原谅我的任何错误或混淆。

对于这项任务,它是一个"记分牌"主题一个。在我们给出值的地方,我们必须按照从最大数字到最小数字的顺序将这些值放入数组中,但是如果分数高于索引中的分数,则还要确保将新分数添加到记分板中[1] ]新分数进入索引[1]并将其他所有内容减少一个。

name.addscore(43)
name.addscore(30)
name.addscore(35)
name.addscore(50)
name.addscore(31)

以上是用户随机输入的分数的示例,而不是按顺序输入。

所以我正在编写一个addScores方法到数组上。我需要使用2个循环。第一个循环应该找到并存储新数字应该去的索引。第二个循环应该移动需要移位的数字。然后,您可以将新分数存储在适当的位置。

如何在Java中执行上述代码?

2 个答案:

答案 0 :(得分:1)

由于您只需要使用数组并且不使用' helper'函数,就像Arrays类中的函数一样。你可以解决这个问题:

public class ScoreBoard{
    Participant[] participants = new Participant[0];

    public void addParticipant(Participant participant){
        System.out.println("Adding participant: " + participant);

        Participant[] oldParticipants = participants;
        participants = new Participant[oldParticipants.length + 1];

        if(oldParticipants.length == 0){
            participants[0] = participant;
            return;
        }

        for(int i = 0; i < oldParticipants.length; i++){
            if(participant.getScore() > oldParticipants[i].getScore()){
                participants[i] = participant;

                for(int j = i; j < oldParticipants.length; j++){
                    participants[j+1] = oldParticipants[j];
                }
                return;
            }

            participants[i] = oldParticipants[i];      
        }

        participants[participants.length-1] = participant;
    }

    public void printScores(){
        System.out.println("\nPrinting current scores...");
        for(int i = 0; i < participants.length; i++){
            System.out.println(participants[i]);
        }
        System.out.println();
    }

    public static void main(String... args){
        System.out.println("Initializing ScoreBoard...\n");
        ScoreBoard sb = new ScoreBoard();

        //Add some participants with scores
        sb.addParticipant(new Participant("Allen", 37));
        sb.addParticipant(new Participant("Bob", 20));
        sb.addParticipant(new Participant("Mark", 54));
        sb.addParticipant(new Participant("Jenny", 77));
        sb.addParticipant(new Participant("Sue", 32));
        sb.addParticipant(new Participant("Dave", 89));
        sb.addParticipant(new Participant("Jeremy", 26));
        sb.addParticipant(new Participant("Jill", 76));
        sb.addParticipant(new Participant("Sarah", 63));
        sb.addParticipant(new Participant("Don", 92));

        sb.printScores();
    }
}

class Participant implements Comparable<Participant>{
    String name;
    int score = 0;

    public Participant(String n){
        this.name = n;
    }

    public Participant(String n, int s){
        this(n);
        score = s;
    }

    public String getName(){
        return this.name;
    }

    public int getScore(){
        return this.score;
    }

    public String toString(){
        return name + " : " + score;
    }

    public int compareTo(Participant other){
        return -this.score.compareTo(other.score);
    }
}

如果您不必使用数组并且可以使用任何数据结构,请尝试以下操作:

public class ScoreBoard{
    TreeSet<Participant> participants = new TreeSet<>();

    public void addParticipant(Participant participant){
        participants.add(participant);
    }

    public void printScores(){
        System.out.println("Printing current scores...");
        for(Participant p: participants){
            System.out.println(p);
        }
    }

    public static void main(String... args){
        System.out.println("Initializing ScoreBoard...");
        ScoreBoard sb = new ScoreBoard();

        //Add some participants with scores
        sb.addParticipant(new Participant("Allen", 37));
        sb.addParticipant(new Participant("Bob", 20));
        sb.addParticipant(new Participant("Mark", 54));
        sb.addParticipant(new Participant("Jenny", 77));
        sb.addParticipant(new Participant("Sue", 32));
        sb.addParticipant(new Participant("Dave", 89));
        sb.addParticipant(new Participant("Jeremy", 26));
        sb.addParticipant(new Participant("Jill", 77));
        sb.addParticipant(new Participant("Sarah", 63));
        sb.addParticipant(new Participant("Don", 92));

        sb.printScores();
    }
}

public class Participant implements Comparable<Participant>{
    String name;
    int score = 0;

    public Participant(String n){
        this.name = n;
    }

    public Participant(String n, int s){
        this(n);
        score = s;
    }

    public String getName(){
        return this.name;
    }

    public int getScore(){
        return this.score;
    }

    public String toString(){
        return name + " : " + score;
    }

    public int compareTo(Participant other){
        return -this.score.compareTo(other.score);
    }
}

答案 1 :(得分:0)

根据我的理解,您正在寻找最大堆数据结构,其中根值是最大值。 您可以查看此链接以获得一些想法。

http://www.sanfoundry.com/java-program-implement-max-heap/