我试图创建某种循环来检查数组中的高分值,如果它高于某些其他值,则将其添加到数组中,并将所有较低的值向下移动。
例如,数组名为Integer highScoreArray[12, 9, 8, 7]
,其中包含4个值。我有另一个数组,保持高分的相应日期称为String highDateArray["12/5/11", "3/4/11", "4,4/12", "6/6/10"]
。
如何向阵列添加新值和相应日期?即如果新值为“10”且日期为“5/29/12”?
我不想在highScoreArray[]
上使用sort命令,因为那时我会忘记highDateArray[]
中相应值的位置。我希望之后在两个单独的线性布局中将两个数组中的两个数组排序,所以试图将它们分开,我是否需要将它变得更加困难?
for (int i = 0; i < highScoreArray.length; i++)
{
if(newHighScore>=highScoreArray[i])
{
// DO SOMETHING
}
}
答案 0 :(得分:3)
如何使用hashmap存储值和相应日期? 并使用值对它们进行排序?
希望这些链接可以帮助您
答案 1 :(得分:1)
您可以创建一个对象并实现Comparable以使其更容易。
public class HighScore implements Comparable {
private int score;
private Date date; // Or you can just use String
// Getters/Setters here
...
public int compareTo(HighScore anotherScore) {
// Checking here
// Return 0 if this and anotherScore is same
// Return 1 if this greater than anotherScore
// Return -1 if this smaller than anotherScore
}
}
所以在你的循环中
for (int i = 0; i < highScoreArray.length; i++) {
HighScore highScore = highScoreArray[i];
if (newHighScore.compareTo(highScore) > 0) {
// Do something
}
}