【问题标题】:android creating a high score checkandroid创建一个高分检查
【发布时间】:2012-05-30 02:33:43
【问题描述】:

我正在尝试创建某种循环来检查数组中的高分值,如果它高于其他一些值,则将其添加到数组中,然后将所有较低的值向下移动。

例如,数组名为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[] 上使用排序命令,因为那样我会忘记highDateArray[] 中对应值的位置。我希望之后在两个单独的线性布局中对两列文本中的两个数组进行排序,因此试图将它们分开,我是否让这变得更加困难?

for (int i = 0; i < highScoreArray.length; i++) 
{
    if(newHighScore>=highScoreArray[i])
    {
        // DO SOMETHING   
    }
}

【问题讨论】:

  • 您是否尝试过创建某种HighScore 对象,该对象的字段为int 用于分数,String 用于日期?不回答你的问题,但它可能会让事情变得更容易;它将保持分数与正确的日期配对,并且您可以让 HighScore 对象实现 Comparable 以便您可以对它们的数组进行排序。

标签: android arrays


【解决方案1】:

如何使用hashmap 存储值和相应的日期? 并使用值对它们进行排序?

希望这些链接对您有所帮助

How to sort hashmap

Sorting HashMap by key

How to sort a Map

【讨论】:

    【解决方案2】:

    您可以创建一个对象并实现 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
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2019-04-16
      • 2021-11-30
      • 2013-10-14
      • 2011-06-15
      • 2017-10-21
      • 1970-01-01
      • 2021-02-26
      • 2015-07-27
      • 1970-01-01
      相关资源
      最近更新 更多