首頁 > Java > 主體

對字串長度排序,但相反(最長的字串在前)

王林
發布: 2024-02-13 23:57:08
轉載
540 人瀏覽過

php小編草莓為你帶來了一個有趣的字符串排序問題:對字符串長度排序,但相反。即最長的字串排在最前面。這個問題可以透過使用內建函數和自訂排序函數來解決。在排序過程中,我們需要計算每個字串的長度,並按照長度從大到小的順序進行排序。接下來,我們將詳細介紹如何實作這個有趣的字串排序問題。

問題內容

我必須做一個練習,我必須寫一個方法 orderquestionsbylength(),它應該按問題的長度(降序)對問題進行排序

以下程式碼有效(但未按降序排序):

`public void orderQuestionsByLength(){
        Collections.sort(questions,Comparator.comparing(question -> question.getText().length())); 
        for (Question question : questions){                                                       
            System.out.println(question);
        }
    }`
登入後複製

一旦我新增.reversed(),我的ide就會拋出錯誤無法解析「物件」中的「gettext」方法即使我有一個方法gettext()並且它在新增.reversed之前就可以工作()

這讓我發瘋,因為我不知道如何解決這個問題,gpt說我的程式碼是正確的(並不是說我應該依賴gpt,但我有另一種方法,可以對整數進行降序排序,我使用了returned() 沒有任何問題

解決方法

有時會發生這種情況,因為comparator.comparing 方法在與.reversed() 連結時無法正確推斷lambda 參數的類型。

要解決此問題,您可以在 lambda 表達式中提供明確類型資訊。以下是如何使用 (string question) -> … 修改 orderquestionsbylength 方法。

public void orderquestionsbylength() {
    collections.sort(questions, comparator.comparing((question question) -> question.gettext().length()).reversed()); 
    for (question question : questions) {                                                       
        system.out.println(question);
    }
}
登入後複製

呼叫 comparator#reversed

注意語法。 使用 comparator.comparing 產生比較器後呼叫 reversed

  • comparator.comparing 是一個 static 方法調用,產生一個實例(物件)。
  • comparator#reversed 是實例方法調用,產生另一個物件。

正如chf的回答中指出的,您可能需要明確傳遞給的參數的資料類型lambda: ( 字串問題 ) -> ….

public void orderquestionsbylength()
{
    collections.sort( 
        questions, 
        comparator
            .comparing( ( string question ) -> question.gettext().length() )  // static method call. 
            .reversed()  // instance method call. 
    ); 
    questions.foreach( system.out :: println ) ;  
}
登入後複製

順便說一句,您可以使用 collections#foreach 和方法參考來折疊 for 循環,如上所示。

完整範例應用程式碼:

package work.basil.example;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

public class Infer
{
    public static void main ( String[] args )
    {
        Comparator < Question > comparator =
                Comparator
                        .comparing ( ( Question question ) -> question.text ( ).length ( ) )
                        .reversed ( );

        ArrayList < Question > questions =
                new ArrayList <> (
                        List.of (
                                new Question ( "alpha" , 1 ) ,
                                new Question ( "b" , 2 ) ,
                                new Question ( "gamma" , 3 )
                        )
                );
        questions.sort ( comparator );
        questions.forEach ( System.out :: println );
    }
}

record Question( String text , int points ) { }
登入後複製

以上是對字串長度排序,但相反(最長的字串在前)的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:stackoverflow.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!