프로그래밍에 익숙하지 않고 날짜 및 시간 속성이있는 객체의 ArrayList를 시간 순서대로 정렬하는 데 어려움이 있습니다. isBefore 메서드에 대해 많은 테스트를 실행했습니다.이 메서드는 어떤 Tweet 개체가 다른 개체보다 먼저 있는지를 결정하고 정상적으로 결정했습니다. 이것은 내 문제가 아마도 ArrayList를 정렬하는 데 사용하는 알고리즘에 있음을 의미합니다. 내가 정렬 방법없이 전체 프로그램을 실행하면자바에서 객체의 ArrayList를 정렬 할 수 없습니다 - 사용하지 못함 Comparator
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;
}
, 내가 출력으로이 얻을 :
나는 "정렬"알고리즘을 사용하는 경우,이 얻을 :
누군가 내가 잘못하고있는 것을 나에게 설명해 주거나 올바른 방향으로 나를 가리킬 수 있다면, 나는 아주 감사 할 것이다. 엘. 감사! 업데이트
: 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;
}
누군가가 내가 사용하는 것을 제안했다가 비교 나는 더-다른 솔루션은 나에게 제시하지 않을 경우 그러나 나는 내가 기술적으로이 과제입니다 (허용하고 생각하지 않는다).
[Sort Java Collection] (https://stackoverflow.com/questions/6957631/sort-java-collection)의 가능한 복제본 –
[둘러보기] (http://stackoverflow.com/tour)를 참조하십시오. 사이트가 작동하는 방식 및 여기에 주제에 관한 질문이 있으며 이에 따라 질문을 편집하십시오. 참고 항목 : [작은 프로그램 디버깅 방법] (https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) –
Collections.sort (yourList, 새 TweetComparator())를 사용하십시오. compartor를 구현하는 방법에 대한이 문서는 https://www.javatpoint.com/Comparator-interface-in-collection-framework – Santosh