![](/img/trans.png)
[英]Compare arrays of custom objects in Swift?
我有實現Сomparable協議的對象: 所以現在我可以對[SomeClass]進行排序。 但是如果我想比較 arrays 這樣的對象呢? Binary operator '<' cannot be applied to two '[SomeClass]' operands 如何解決這個問題 ...
[英]Compare arrays of custom objects in Swift?
我有實現Сomparable協議的對象: 所以現在我可以對[SomeClass]進行排序。 但是如果我想比較 arrays 這樣的對象呢? Binary operator '<' cannot be applied to two '[SomeClass]' operands 如何解決這個問題 ...
[英]How can I implement a PriorityStack that accepts objects with the Comparable interface?
我看了一下這個線程中的代碼: https://codereview.stackexchange.com/questions/98105/priority-stack-in-java 這使用 object 和優先級實現了一個 PriorityStack。 我需要一個實現,您只需將 object 添加 ...
[英]Generic Comperable Type Java - Error if I set the function to static
晚上好, Java 發生了一些古怪的事情。我將是第一個承認我在 Java 上很爛的人,但我真的無法理解這個問題,也許有人可以提供幫助。 我正在嘗試制作一個 class 來執行 GnomeSort(一些小眾排序方法,只是為了練習 Java)。 這是我構建的... gnomeSort 方法,出於某種 ...
[英]How can multiple static methods use the same generic type?
在學習算法(第 4 版)(作者:Robert Sedgewick;Kevin Wayne)時,我正在嘗試重新創建他們的代碼。 他們原始使用參數化構造函數(泛型),我認為這可能是因為 2011 Java 代碼與今天的 Java 代碼不同。 這是一個例子: 這適用於 Java 11/18。 但問題是我 ...
[英]Creating generic comparator for List<List<Comparable>>
我正在嘗試為List<List<Comparable>>創建一個通用比較器,它可以對多個字段進行排序。 該結構表示一個行列表,在每一行中,我想對多列進行排序。 我有這個 但它似乎只適用於第一個排序字段,而不適用於多個字段。 例如,這是按第 0 列正確排序的,但不是第 1 列 ...
[英]Comparable custom types which can be sorted as array items without defining an additional compare function inside sort()
Python 和 Java 等語言具有對自定義類進行排序的特殊方法。 在 JavaScript 中, toString()可以被覆蓋,但這對於數值來說並不容易。 作為解決方法,我向類添加了一個名為compareTo()的方法,盡管這仍然需要一個函數來調用它。 class NumericSorta ...
[英]Ensure strict comparability at compile time in Go 1.20?
在 Go 1.18 和 Go 1.19 中,我可以在編譯時確保類型是嚴格可比較的,即它支持==和!=運算符,並且保證在運行時不會出現恐慌。 例如,這對於避免無意中將字段添加到可能導致不必要的恐慌的結構很有用。 我只是嘗試實例化與之comparable的對象:// supports == and ! ...
[英]Dijkstra's weighted shortest path in Python
我正在嘗試解決來自 PepCoding、Graph Foundation 1、Shortest Path In Weights 的問題,並將解決方案從 Java 復制到 Python。 問題: 示例輸入: Output: 我實現了一個列表而不是 PriorityQueue,並嘗試使用__lt__和 ...
[英]How do sort Entries of the Map<Type,List<String>> by Value and store into a LinkedHashMap
我有以下Item class: 並且有一個項目List ,如下所示: 我需要按Type對Item進行分組並存儲到 map 中。 然后我需要按升序對map 條目按Value (在本例中是String的List )進行排序並存儲到LinkedHashMap 。 如何根據List<String&g ...
[英]Java Comparable Shape
我的代碼有問題。 我有抽象的 class 形狀實現了 Comparable,以及像矩形等類。我想對我的形狀列表進行排序。 一個按區域,如果我調用方法,它將按名稱,例如.. 怎么做? 這很好。 一切正常,但如何在 Main 中調用如下所示:Shape.SortBy(Shape.NAME) 及其方法現在 ...
[英]use compareTo on more specific Comparables in Java
我想要一個更具體的 Comparable 接口,以便我可以更好地鍵入返回參數。 目前的界面是這樣的: 現在,當我嘗試對這些更具體的 Comparables 列表進行排序時,如下所示,我收到一個錯誤,因為 b 是“RankComparable”類型而不是“T”類型。 為了解決這個問題,我可以實現一個 ...
[英]prog.java:59: error: incompatible types: bad return type in lambda expression ((p1,p2)->(p2.ratio-p1.ratio));
我正在嘗試創建一個包含雙精度和整數類型對的優先級隊列,但出現此錯誤: 這是相關代碼 ...
[英]Could java enum class implements "Comparable" interface?
似乎枚舉類已經有一個final的 compareTo 函數,它不能被覆蓋。 但我的要求是像這樣自定義一個枚舉類: 然后我希望它實現 Comparable 接口以便能夠在Add 、 Sub 、 Mul等之間進行比較。但似乎我不能如上所述使用implements Comparable<Oper ...
[英]Collections.sort() doesn't work. Problem with implementing the Comparable<> interface
有人知道為什么這段代碼不能正確地對員工進行排序嗎? 我需要他們按工資金額升序排列。 我想我搞砸了,因為我將工資存儲在雙打中。 但我真的不知道該怎么辦。 請幫忙。 輸出: 這也不起作用 ...
[英]ThreadPool throws java.util.concurrent.FutureTask cannot be cast to java.lang.Comparable
我有一個非常簡單的程序: 在使用 maven 運行它時,它會打印: 並掛起。 我的程序中沒有PriorityBlockingQueue的任何“可比較”實現,這個異常是從哪里來的? ...
[英]Sorting an array of zeros, negative and positive numbers with Comparable interface
我想根據 y 坐標對數組進行排序:但是混合了正負 y 值,這給了我錯誤(未排序)的順序 例如:對於 I/P:[[-2147483646,-2147483645], [2147483646,2147483647]] 我首先需要 [-2147483646,-2147483645] 然后是 [2147 ...
[英]Why get class cannot be cast to class java.lang.Comparable
我正在使用 java 8 sorted()的自然排序方法迭代所有Student數據。 迭代學生數據時,IDE 控制台class com.java8.Student cannot be cast to class java.lang.Comparable 。 我的StreamStudent.java文 ...
[英]As per TreeSet two objects are shown equal but Queue shows them as unequal
我有以下 Person 類- Person.java - 現在,我有以下 TreeClass 實例 - 打印時—— 很明顯,兩個 Person 實例 - new Person(4, "Amrita")和new Person(4, "Amrita")是相等的。 現在,我有以下隊列代碼 ...
[英]Unable to understand the logic behind treesorting in this particular case of mine
我有以下“人” class - 現在在主要課程中,我制作了TreeClass - 打印后的輸出是 - 一切都是正確的,除了“Nipun”和“Nipuna”被認為是平等的。 我哪里錯了? ...
[英]Generic method with Comparable does not accept Integer[]
問題 我有一堂課叫 public class HeapClass <E extends Comparable <E>> 里面有個方法叫 public void heapSort(E[] arr) 我有一個小的main方法來測試它,並想用一個簡單的數組調用該方法: In ...