{"id":264,"date":"2020-02-05T09:37:44","date_gmt":"2020-02-05T01:37:44","guid":{"rendered":"https:\/\/twocups.cn\/?p=264"},"modified":"2021-03-21T18:17:40","modified_gmt":"2021-03-21T10:17:40","slug":"18","status":"publish","type":"post","link":"https:\/\/twocups.cn\/index.php\/2020\/02\/05\/18\/","title":{"rendered":"\u3010Java\u3011\u91cd\u5199\u6bd4\u8f83\u5668java.util.Comparator\u7684\u6ce8\u610f\u4e8b\u9879"},"content":{"rendered":"\n<h4><img width=\"80\" src=\"https:\/\/twocups.cn\/wp-content\/uploads\/2019\/12\/logo\u6a2a.png\" alt=\"\" class=\"wp-image-152\" srcset=\"https:\/\/twocups.cn\/wp-content\/uploads\/2019\/12\/logo\u6a2a.png 3337w, https:\/\/twocups.cn\/wp-content\/uploads\/2019\/12\/logo\u6a2a-300x125.png 300w, https:\/\/twocups.cn\/wp-content\/uploads\/2019\/12\/logo\u6a2a-1024x426.png 1024w, https:\/\/twocups.cn\/wp-content\/uploads\/2019\/12\/logo\u6a2a-768x320.png 768w, https:\/\/twocups.cn\/wp-content\/uploads\/2019\/12\/logo\u6a2a-1536x639.png 1536w, https:\/\/twocups.cn\/wp-content\/uploads\/2019\/12\/logo\u6a2a-2048x852.png 2048w\" sizes=\"(max-width: 3337px) 100vw, 3337px\" \/>\u7248\u6743\u58f0\u660e\uff1a\u672c\u6587\u4e3a\u535a\u4e3b\u539f\u521b\u6587\u7ae0\uff0c\u8f6c\u8f7d\u8bf7\u6ce8\u660e\u51fa\u5904\uff1a<a href=\"https:\/\/twocups.cn\/index.php\/2020\/02\/05\/18\/\">https:\/\/twocups.cn\/index.php\/2020\/02\/05\/18\/<\/a><\/h4>\n\n\n\n<p>\u6709\u65f6\u6211\u4eec\u9700\u8981\u5bf9\u540c\u4e00\u7c7b\u578b\u7684\u5bf9\u8c61\u8fdb\u884c\u591a\u79cd\u4e0d\u540c\u65b9\u5f0f\u7684\u6392\u5e8f\uff0c\u800c\u81ea\u7136\u6392\u5e8f Comparable \u5e76\u4e0d\u80fd\u5b9e\u73b0\u3002\u8fd9\u91cc\u6211\u4eec\u5c31\u9700\u8981<strong>\u91cd\u5199\u6bd4\u8f83\u5668<\/strong>\uff0c\u53ef\u4ee5\u8ba9\u5bf9\u8c61\u6570\u7ec4\u6216\u96c6\u5408\u4ee5\u81ea\u5b9a\u4e49\u7684\u6392\u5e8f\u89c4\u5219\u8fdb\u884c\u6392\u5e8f\u3002<\/p>\n\n\n\n<h2>\u793a\u4f8b<\/h2>\n\n\n\n<p>\u7ed9\u5b9a\u4e00\u7ec4\u975e\u8d1f\u6574\u6570\uff0c\u91cd\u65b0\u6392\u5217\u5b83\u4eec\u7684\u987a\u5e8f\u4f7f\u4e4b\u7ec4\u6210\u4e00\u4e2a\u6700\u5927\u7684\u6574\u6570\u3002\u4f8b\u5982 [10, 2] \u7684\u8f93\u51fa\u4e3a210\uff0c[3, 30, 34, 5, 9] \u7684\u8f93\u51fa\u4e3a9534330\u3002<\/p>\n\n\n\n<p>\u8fd9\u9053\u9898\u89e3\u6cd5\u7684\u5173\u952e\u5c31\u662f\u6392\u5e8f\uff0c\u5177\u4f53\u7684\u89c4\u5219\u4e3a\uff1a\u6570\u7ec4\u4e2d\u7684\u975e\u8d1f\u6574\u6570\uff08\u4f8b\u5982 <strong>x<\/strong> \u548c <strong>y<\/strong>\uff09\u4e24\u4e24\u8fdb\u884c\u6bd4\u8f83\uff0c\u82e5<strong>x + y &gt; y + x<\/strong>\uff0c\u5219\u5224\u5b9a <strong>x <\/strong>\u7684\u4f18\u5148\u7ea7\u6bd4 <strong>y<\/strong> \u9ad8\uff0c\u4e14 <strong>x<\/strong> \u5728\u6700\u7ec8\u7684\u6574\u6570\u91cc\u6392\u5728 <strong>y<\/strong> \u7684\u524d\u9762\u3002<\/p>\n\n\n\n<h2>\u4ee3\u7801<\/h2>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"classic\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import java.util.Arrays;\nimport java.util.Comparator;\n\nclass Test{\n    public String largestNumber(int[] nums) {\n        Integer[] arr = new Integer[nums.length];\n        for(int i = 0; i &lt; nums.length; i++) arr[i] = nums[i];\n        Arrays.sort(arr, new Comparator&lt;Integer>(){\n            @Override\n            public int compare(Integer o1, Integer o2){\n                String s1 = o1 + \"\" + o2;\n                String s2 = o2 + \"\" + o1;\n                return s2.compareTo(s1);\n            }\n        });\n        StringBuffer sb = new StringBuffer();\n        for(int x: arr) sb.append(x);\n        String res = sb.toString();\n        return res.charAt(0) == '0' ? \"0\" : res;\n    }\n}<\/pre>\n\n\n\n<p>\u4ee3\u7801\u4e2d\u6211\u4eec\u4f7f\u7528\u4e86\u6570\u7ec4\u7684\u6392\u5e8f\u65b9\u6cd5 sort()\uff0c\u5e76\u4e14\u91cd\u5199\u4e86\u4e00\u4e2a\u6bd4\u8f83\u5668 Comparator \u6765\u5b9a\u4e49\u6570\u7ec4\u4e2d\u5143\u7d20\u5728\u6392\u5e8f\u65f6\u4e24\u4e24\u4e4b\u95f4\u7684\u6bd4\u8f83\u89c4\u5219\u3002\u5982\u679c\u662f\u96c6\u5408\uff0c\u540c\u6837\u4e5f\u53ef\u4ee5\u4f7f\u7528 Collections \u7684\u6392\u5e8f\u65b9\u6cd5 sort()\u3002\u6709\u4e9b\u4eba\u5728\u4f7f\u7528\u8fd9\u4e2a\u65b9\u6cd5\u7684\u65f6\u5019\u4f1a\u9047\u5230\u5982\u4e0b\u95ee\u9898\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\" data-enlighter-theme=\"classic\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\u82f1\u6587\u7248\uff1a\nError: java: no suitable method found for sort(char[],&lt;anonymous java.util.Comparator&lt;java.lang.Character>>)\n    method java.util.Arrays.&lt;T>sort(T[],java.util.Comparator&lt;? super T>) is not applicable\n      (inference variable T has incompatible bounds\n        equality constraints: char\n        upper bounds: java.lang.Character,java.lang.Object)\n    method java.util.Arrays.&lt;T>sort(T[],int,int,java.util.Comparator&lt;? super T>) is not applicable\n      (cannot infer type-variable(s) T\n        (actual and formal argument lists differ in length))\n\u4e2d\u6587\u7248\uff1a\nError: java: \u5bf9\u4e8esort(char[],&lt;\u533f\u540djava.util.Comparator&lt;java.lang.Character>>), \u627e\u4e0d\u5230\u5408\u9002\u7684\u65b9\u6cd5\n    \u65b9\u6cd5 java.util.Arrays.&lt;T>sort(T[],java.util.Comparator&lt;? super T>)\u4e0d\u9002\u7528\n      (\u63a8\u8bba\u53d8\u91cf T \u5177\u6709\u4e0d\u517c\u5bb9\u7684\u9650\u5236\u8303\u56f4\n        \u7b49\u5f0f\u7ea6\u675f\u6761\u4ef6: char\n        \u4e0a\u9650: java.lang.Character,java.lang.Object)\n    \u65b9\u6cd5 java.util.Arrays.&lt;T>sort(T[],int,int,java.util.Comparator&lt;? super T>)\u4e0d\u9002\u7528\n      (\u65e0\u6cd5\u63a8\u65ad\u7c7b\u578b\u53d8\u91cf T\n        (\u5b9e\u9645\u53c2\u6570\u5217\u8868\u548c\u5f62\u5f0f\u53c2\u6570\u5217\u8868\u957f\u5ea6\u4e0d\u540c))<\/pre>\n\n\n\n<p>\u539f\u56e0\u662f <strong>public static void sort (T [] a\uff0cComparator c)<\/strong> \u662f\u6839\u636e\u6307\u5b9a\u6bd4\u8f83\u5668\u5f15\u53d1\u7684\u987a\u5e8f\u5bf9\u6307\u5b9a\u7684\u5bf9\u8c61\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\uff0c\u6240\u4ee5 sort() \u65b9\u6cd5\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570 <strong>T [] a<\/strong> \u4e2d\u53ea\u80fd\u5b58\u653e\u5bf9\u8c61\uff0c\u800c\u4e0d\u80fd\u5b58\u653e\u57fa\u672c\u6570\u636e\u7c7b\u7684\u6570\u636e\u3002\u5982\u679c\u5c06 char \u7c7b\u578b\u7684\u6570\u7ec4\u4f20\u5165\u8be5\u65b9\u6cd5\uff0c\u5219\u4f1a\u62a5\u51fa\u4ee5\u4e0a\u9519\u8bef\uff1b\u5982\u679c\u4f20\u5165\u7684\u662f Character \u7c7b\u578b\u7684\u6570\u7ec4\uff0c\u5219\u6b63\u5e38\u3002<\/p>\n\n\n\n<p>\u8fd9\u91cc\u53ef\u80fd\u6709\u4eba\u8981\u95ee\u4e86\uff0cchar \u7c7b\u578b\u7684\u6570\u636e\u548c Character \u7c7b\u578b\u7684\u6570\u636e\u4e0d\u662f\u4f1a\u81ea\u52a8\u88c5\u7bb1\u548c\u5c01\u7bb1\u5417\uff0c\u90a3\u4e3a\u4ec0\u4e48\u8fd9\u91cc\u4e24\u8005\u7684\u6570\u7ec4\u4e0d\u80fd\u901a\u7528\u5462\uff1f<\/p>\n\n\n\n<p><strong>\u901a\u7528\u4e0d\u4ee3\u8868\u6df7\u7528\u3002<\/strong>char \u5728 Java \u4e2d\u5c5e\u4e8e\u516b\u5927\u57fa\u672c\u6570\u636e\u7c7b\u578b\uff0c\u4e0d\u9700\u8981\u5b9e\u4f8b\u5316\u5c31\u80fd\u76f4\u63a5\u4f7f\u7528\uff1b\u800c Character \u662f char \u7684\u5c01\u88c5\u7c7b\uff0c\u9700\u8981\u5b9e\u4f8b\u5316\u540e\u624d\u80fd\u7528\u3002\u4e00\u822c\u6765\u8bf4\uff0cchar \u7c7b\u578b\u7684\u6570\u636e\u548c Character \u7c7b\u578b\u7684\u6570\u636e\u4f1a\u52a8\u6001\u5730\u8fdb\u884c\u81ea\u52a8\u5c01\u7bb1\u548c\u88c5\u7bb1\uff0c\u6765\u6ee1\u8db3\u4e0d\u540c\u7684\u9700\u6c42\uff0c\u4f46\u662f char \u7c7b\u578b\u7684\u6570\u7ec4\u548c Character \u7c7b\u578b\u7684\u6570\u7ec4\u5e76\u4e0d\u4f1a\u81ea\u52a8\u8f6c\u6362\u554a\uff0c\u5b83\u4eec\u7684\u672c\u8d28\u662f\u6570\u7ec4\uff0c\u53ea\u662f\u5b58\u653e\u4e86\u4e24\u79cd\u7c7b\u578b\u7684\u6570\u636e\u800c\u5df2\u3002<\/p>\n\n\n\n<p>\u6240\u4ee5\u5982\u679c\u4e4b\u524d\u5f97\u5230\u7684\u662f char \u7c7b\u578b\u7684\u6570\u7ec4\uff0c\u9700\u8981\u6211\u4eec\u81ea\u5df1\u65b0\u5efa\u5176\u5c01\u88c5\u7c7b\u6570\u7ec4\uff0c\u5e76\u5c06 char \u6570\u7ec4\u7684\u5143\u7d20\u5168\u90e8\u88c5\u5165\u5176\u5c01\u88c5\u7c7b\u6570\u7ec4\u4e2d\uff08\u8fd9\u91cc\u5c06\u5143\u7d20\u88c5\u5165\u65f6\u624d\u4f1a\u81ea\u52a8\u5c01\u7bb1\uff09\u3002<\/p>\n\n\n\n<p>\u5176\u5b9e\uff0c\u5728\u7279\u6b8a\u60c5\u51b5\u4e0b\u8fd8\u6709\u66f4\u52a0\u65b9\u4fbf\u7684\u5199\u6cd5\uff0c\u6211\u4e3e\u4e24\u4e2a\u4f8b\u5b50\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"classic\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ \u5c06\u6392\u5e8f\u7684\u53c2\u7167\u8981\u7d20\u4ece\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u53d8\u6210\u8be5\u5143\u7d20\u7684\u67d0\u4e2a\u90e8\u5206\nint[] nums = new int[]{{2, 5}, {3, 7}, {1, 5}, {9, 1}};\nArrays.sort(nums , Comparator.comparingInt(a -> a[0]));\n\/\/ \u6539\u53d8\u6392\u5e8f\u6240\u4f9d\u7167\u7684\u4e24\u4e2a\u5143\u7d20\u7684\u6bd4\u8f83\u65b9\u5f0f\nString[] strs = new String[]{\"aaa\", \"bbb\"};\nArrays.sort(strs , (x, y) -> y.compareTo(x));\n\n\/\/ \u4f18\u5148\u961f\u5217\uff1a\u7531\u539f\u6765\u7684\u5347\u5e8f\u6392\u5e8f\uff0c\u5230\u73b0\u5728\u7684\u6839\u636e\u5143\u7d20\u7684time\u5c5e\u6027\u964d\u5e8f\u6392\u5e8f\nPriorityQueue&lt;Integer> priorityQueue = new PriorityQueue&lt;>((a, b) -> (b.time - a.time));<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u7248\u6743\u58f0\u660e\uff1a\u672c\u6587\u4e3a\u535a\u4e3b\u539f\u521b\u6587\u7ae0\uff0c\u8f6c\u8f7d\u8bf7\u6ce8\u660e\u51fa\u5904\uff1ahttps:\/\/twocups.cn\/index.php\/202 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":67,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[80],"tags":[85,32,84],"_links":{"self":[{"href":"https:\/\/twocups.cn\/index.php\/wp-json\/wp\/v2\/posts\/264"}],"collection":[{"href":"https:\/\/twocups.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/twocups.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/twocups.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/twocups.cn\/index.php\/wp-json\/wp\/v2\/comments?post=264"}],"version-history":[{"count":0,"href":"https:\/\/twocups.cn\/index.php\/wp-json\/wp\/v2\/posts\/264\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/twocups.cn\/index.php\/wp-json\/wp\/v2\/media\/67"}],"wp:attachment":[{"href":"https:\/\/twocups.cn\/index.php\/wp-json\/wp\/v2\/media?parent=264"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/twocups.cn\/index.php\/wp-json\/wp\/v2\/categories?post=264"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/twocups.cn\/index.php\/wp-json\/wp\/v2\/tags?post=264"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}