将元素插入到排序列表中

时间:2012-12-08 10:25:45

标签: java android arrays logic sorting

好的我正在使用getSharedPreferences存储我的高分,但在我填写之前我想通过和数组按升序对分数进行排序,但是如果它在第一个pos中找到的分数小于它,那么它就不会检查剩下的最小的?

    //function to add score to array and sort it
    public void addscoretoarray(int mScore){
    for(int pos = 0; pos< score.length; pos++){
        if(score[pos]  > mScore){
            //do nothing
        }else {
                //Add the score into that  position
                score[pos] = mScore;
                break;
            }
    }
    sortArray(score);
}

我应该在循环之前和之后调用sortArray()来解决这个问题,还是有更好的方法来实现相同的结果?

我还应该提一下sortArray(score)函数只是调用Arrays.sort(得分) 得分是mScore数组

编辑: 基于@Vincent Ramdhanie发布的内容,我修改了帖子:

    public void addscoretoarray(int mScore){
    int pos = score.length; 
    //sort the array (in ascending order)
    sortArray(score);

    //go though the array( in descending order) and check for a place that suits the conditions
    while(pos>=0 && score[pos] > mScore){ 
         pos--; //do nothing as score[pos] is larger than mScore
    }
     //so once a pos is found (e.g. broke out of the while loop)
     //check that it is still in the list
    if(pos >= 0){
        //if it is then move everything down 1 position
        for(int i = 0; i < pos; i++){
            score[i] = score[i+1];
        }
        //replace the initial pos with the new score
        score[pos] = mScore;
    }
}

我仍然相信它会在for(int i = 0; i < pos; i++){循环中从列表中删除。

4 个答案:

答案 0 :(得分:5)

如果我理解正确,那么我建议

    int[] a1 = { 1, 2, 3, 4, 6 };
    int mScore = 5;

    int[] a2 = new int[a1.length + 1];
    Arrays.sort(a1);
    int p = Arrays.binarySearch(a1, mScore);
    if (p < 0) {
        p = -p - 1;
        System.arraycopy(a1, 0, a2, 0, p);
        System.arraycopy(a1, p, a2, p + 1, a1.length - p);
        a2[p] = mScore;
    }
    System.out.println(Arrays.toString(a2));

输出

[1, 2, 3, 4, 5, 6]

请注意,它只插入唯一值

答案 1 :(得分:3)

为什么不对分数数组进行排序。因此,您对数组的添加分数将假定数组始终按降序排序。要插入的新分数将简单地在插入时从阵列中取出最低分。然后,您可以使用类似这样的插入算法:

   insertScore(int[] scores, int mscore){
        //find insert point
        int i = 0;
        while(i < scores.length && scores[i] > mscore){
            i++;
        }
        if(i < scores.length){
            //you found a place to insert the score
            for(int j = scores.length-1; j > i; j--){
                scores[j] = scores[j - 1];
            }
            scores[i] = mscore;
        }
   }

在这种情况下,无需使用数组。

答案 2 :(得分:1)

请参阅javadoc to @return of binarySearch

  

返回搜索键的索引(如果它包含在列表中); 否则,( - (插入点) - 1)。插入点定义为键将插入列表的点:第一个元素的索引大于键,或者list.size(),如果列表中的所有元素都小于指定的键。请注意,当且仅当找到密钥时,这可以保证返回值>> =。

答案 3 :(得分:0)

public void addscoretoarray(int mScore){
    for(int pos = 0; pos< score.length; pos++){
        if(score[pos]  > mScore){
            //do nothing
        }else {
                //Add the score into that  position
                score[pos] = mScore;
                break;
            }
    }
    sortArray(score);
}

代码中存在一些重大错误。

  1. score[pos] = mScore; 在此声明中,您将在mScore位置分配pos,这将导致pos存储的值丢失。

  2. 如果您正在使用数组,那么要在其间存储任何元素,您需要将所有剩余元素向右移动1个位置,而不是在此处。

  3. score[pos] = mScore; break;

  4. 在将元素存储在pos。

    之后,break将在第一次迭代中打破循环

    建议:

    使用arraylist而不是本机数组。 修改后的伪代码:

    public void addscoretoarray(int mScore){
        int index = getFirstIndexOfScoreGreaterThanmScore(); // need to implement it
        if(index == -1){ // no element greater than mscore
        score.add(mScore);
        }else{
        score.add(index,mScore);
    }
    //   sortArray(score); // no need to call this if the list is initially empty as the insertion will be in sorted order itself
    if(score.length == maxSize){
    //do whateverwhen the list is full as per your requirements
    }
    }