【问题标题】:cannot sort an ArrayList of objects in java--Not allowed to use Comparator无法在 java 中对对象的 ArrayList 进行排序--不允许使用 Comparator
【发布时间】:2017-12-06 06:18:30
【问题描述】:

我是编程新手,在将具有日期和时间属性的对象的 ArrayList 排序为时间顺序时遇到了一些困难。我对我的 isBefore 方法进行了许多测试,该方法确定哪个 Tweet 对象在另一个之前,并确定它没问题。这意味着我的问题可能在于我用来对 ArrayList 进行排序的算法。

 private void sortTwitter(){
  ArrayList<Tweet> temp = new ArrayList<Tweet>();

   while(getSizeTwitter()>1){

     int indexOfEarliest = 0;


     for(int i = 0; i<getSizeTwitter(); i++){ //finds the index of the earliest tweet in the tweets ArrayList

       //The Tweet object at [0] is tweets.get(0).
       //The Tweet object we want to compare it to initially is tweets.get(1). Specifically, we want tweets.get(1).getDate() and .getTime()
       Tweet tweet1 = this.tweets.get(indexOfEarliest);
       Tweet tweet2 = this.tweets.get(i);
       boolean tweet1Earlier = tweet1.isBefore(tweet2.getDate(), tweet2.getTime());

       if(tweet1Earlier == false){ //Supposed to update the value of indexOfEarliest to the index of tweet2 is tweet2 is earlier.
         indexOfEarliest = i;
       } 

     }

     temp.add(this.tweets.get(indexOfEarliest));
     this.tweets.remove(indexOfEarliest);
   }

   //Now tweets is an array of 1 element, the largest.
   temp.add(this.tweets.get(0));
   this.tweets.clear();
   this.tweets = temp;

  } 

如果我在没有排序方法的情况下运行完整程序,我会得到以下输出:

Without "sorting"

如果我使用“排序”算法,我会得到:

With "sorting"

如果有人可以向我解释我做错了什么或指出我正确的方向,我将不胜感激。谢谢!

更新:isBefore

public boolean isBefore(String sTestDate, String sTestTime){

    String[] splitTweetDate = this.date.split("-"); //Reconfigures the format of Tweet instance date and time
    String[] splitTweetTime = this.time.split(":");
    int[] tweetDate = stringToInt(splitTweetDate);
    int[] tweetTime = stringToInt(splitTweetTime);


    String[] splitTestDate = sTestDate.split("-"); //Reconfigures the format of query parameters
    String[] splitTestTime = sTestTime.split(":");
    int[] testDate = stringToInt(splitTestDate);
    int[] testTime = stringToInt(splitTestTime);

    for(int i = 0; i<3; i++){ //Tests the date year first, then month, then day. Returns true if tweetDate is earlier than testDate. 
      if(tweetDate[i]<testDate[i]){
        return true;
      }
    }

    for(int i = 0; i<3; i++){ //If on the same day, tests hour first, then minutes, then seconds. Returns true if tweetTime is earlier than testDate.
      if(tweetTime[i]<testTime[i]){
        return true;
      }
    }

    return false; //If method returns nothing, message was posted at the same time or later.

  }

  //HELPER 2
  private int[] stringToInt(String[] arrString){ //converts an array of strings into an array of integers.
    int[] arrInt = new int[arrString.length];

    for(int i = 0; i<arrInt.length; i++){ //parseing the elements of the string array to integers.
      arrInt[i] = Integer.parseInt(arrString[i]);
    }

    return arrInt;
  }

有人建议我使用比较,如果没有其他解决方案提供给我,我会使用比较,但我认为技术上不允许我这样做(这是为了分配)。

【问题讨论】:

标签: java sorting arraylist


【解决方案1】:

使用java.util.Comparator创建单独的排序机制,并通过比较java.util.Date提供标准,只需调用Collection.sort(&lt;customList&gt;, Comparator&lt;&gt;);就可以了,无需手动循环。请查看示例如何使用java.util.Date 实现比较器,您会有所了解。它也会减少你的代码行数。

【讨论】:

  • 我意识到我没有在作业的上下文中指定,我不允许使用 Comparator,因为我们还没有在课堂上学习它。感谢您提供非常有效的解决方案,但这不是我被允许实施的解决方案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-07
  • 2017-12-12
  • 2014-11-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多