4

好的,我正在使用 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(score) 其中 score 是 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

4 回答 4

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]

请注意,它仅插入唯一值

于 2012-12-08T13:32:26.007 回答
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;
        }
   }

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

于 2012-12-08T10:41:59.797 回答
2

请参阅javadoc 到 binarySearch 的 @return

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

于 2015-10-06T20:34:56.310 回答
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;

在将元素存储在 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
}
}
于 2012-12-08T11:01:13.947 回答