{"id":204,"date":"2024-01-03T21:27:33","date_gmt":"2024-01-03T13:27:33","guid":{"rendered":"https:\/\/wangqianming.top\/?p=204"},"modified":"2024-01-03T21:27:33","modified_gmt":"2024-01-03T13:27:33","slug":"java%e4%b8%adstringarrayscollections%e7%b1%bb%e4%b8%ad%e7%9a%84%e9%9d%99%e6%80%81%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/wangqianming.top\/index.php\/2024\/01\/03\/java%e4%b8%adstringarrayscollections%e7%b1%bb%e4%b8%ad%e7%9a%84%e9%9d%99%e6%80%81%e6%96%b9%e6%b3%95\/","title":{"rendered":"Java\u4e2dString,Arrays,Collections\u7c7b\u4e2d\u7684\u9759\u6001\u65b9\u6cd5"},"content":{"rendered":"<h1>\u603b\u7ed3Java\u4e2dString,Arrays,Collections\u7c7b\u4e2d\u7684\u9759\u6001\u65b9\u6cd5<\/h1>\n<p>\u5728Java\u4e2d\uff0c<code>String<\/code>\u3001<code>Arrays<\/code> \u548c <code>Collections<\/code> \u662f\u975e\u5e38\u5e38\u7528\u7684\u7c7b\uff0c\u6bcf\u4e2a\u7c7b\u90fd\u6709\u5176\u72ec\u7279\u7684\u4e00\u7cfb\u5217\u9759\u6001\u65b9\u6cd5\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5f88\u65b9\u4fbf\u6211\u4eec\u7528\u4e8e\u5feb\u901f\u5904\u7406\u8fd9\u4e9b\u6570\u636e\u7ed3\u6784\u3002\u76f8\u8f83\u4e8e\u4e00\u4e9b\u52a8\u6001\u65b9\u6cd5\uff0c\u5b83\u4eec\u4f7f\u7528\u7684\u9891\u7387\u504f\u4f4e\uff0c\u4f46\u9700\u8981\u7528\u5230\u65f6\u4f1a\u5f88\u7701\u5fc3\u3002<\/p>\n<h3>String \u7c7b<\/h3>\n<p>\u867d\u7136 <code>String<\/code> \u7c7b\u4e3b\u8981\u63d0\u4f9b\u4e86\u5b9e\u4f8b\u65b9\u6cd5\u6765\u64cd\u4f5c\u5b57\u7b26\u4e32\uff0c\u4f46\u662f\u5b83\u4e5f\u6709\u4e00\u4e9b\u6709\u7528\u7684\u9759\u6001\u65b9\u6cd5\u3002\u4ee5\u4e0b\u662f\u5176\u4e2d\u4e00\u4e9b\u4f8b\u5b50\uff1a<\/p>\n<ul>\n<li>\n<p><code>String.valueOf(\u53c2\u6570)<\/code>\uff1a\u5c06\u4e0d\u540c\u7c7b\u578b\u7684\u6570\u636e\u8f6c\u6362\u6210\u5b57\u7b26\u4e32\u3002<\/p>\n<pre><code class=\"language-java\">int num = 100;\nString str = String.valueOf(num); \/\/ \"100\"<\/code><\/pre>\n<\/li>\n<li>\n<p><code>String.format(\u683c\u5f0f, \u53c2\u6570...)<\/code>\uff1a\u6839\u636e\u683c\u5f0f\u5b57\u7b26\u4e32\u548c\u53c2\u6570\u751f\u6210\u65b0\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<pre><code class=\"language-java\">String formattedString = String.format(\"Hi, %s. Today is %s\", \"Alice\", \"sunny\");\n\/\/ \"Hi, Alice. Today is sunny\"<\/code><\/pre>\n<\/li>\n<\/ul>\n<h3>Arrays \u7c7b<\/h3>\n<p><code>Arrays<\/code> \u7c7b\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u9759\u6001\u65b9\u6cd5\u6765\u64cd\u4f5c\u6570\u7ec4\uff0c\u5305\u62ec\u6392\u5e8f\u3001\u641c\u7d22\u3001\u8f6c\u6362\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684\u65b9\u6cd5\uff1a<\/p>\n<ul>\n<li>\n<p><code>Arrays.sort(\u6570\u7ec4)<\/code>\uff1a\u5bf9\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\u3002<\/p>\n<pre><code class=\"language-java\">int[] arr = {3, 1, 4, 1, 5, 9};\nArrays.sort(arr);\n\/\/ arr \u73b0\u5728\u662f [1, 1, 3, 4, 5, 9]<\/code><\/pre>\n<\/li>\n<li>\n<p><code>Arrays.asList(\u6570\u7ec4)<\/code>\uff1a\u5c06\u6570\u7ec4\u8f6c\u6362\u4e3a\u5217\u8868\u3002<\/p>\n<pre><code class=\"language-java\">String[] strArray = {\"apple\", \"banana\", \"cherry\"};\nList<String> list = Arrays.asList(strArray);\n\/\/ list \u662f\u4e00\u4e2a\u5305\u542b \"apple\", \"banana\", \"cherry\" \u7684\u5217\u8868<\/code><\/pre>\n<\/li>\n<li>\n<p><code>Arrays.copyOf(\u539f\u6570\u7ec4, \u65b0\u957f\u5ea6)<\/code>\uff1a\u590d\u5236\u6570\u7ec4\u5230\u65b0\u7684\u6570\u7ec4\u3002<\/p>\n<pre><code class=\"language-java\">int[] original = {1, 2, 3};\nint[] copied = Arrays.copyOf(original, 5);\n\/\/ copied \u662f [1, 2, 3, 0, 0]<\/code><\/pre>\n<\/li>\n<li>\n<p><code>Arrays.equals(\u6570\u7ec41, \u6570\u7ec42)<\/code>\uff1a\u6bd4\u8f83\u4e24\u4e2a\u6570\u7ec4\u662f\u5426\u76f8\u7b49\u3002<\/p>\n<pre><code class=\"language-java\">int[] a = {1, 2, 3};\nint[] b = {1, 2, 3};\nboolean isEqual = Arrays.equals(a, b); \/\/ true<\/code><\/pre>\n<\/li>\n<\/ul>\n<h3>Collections \u7c7b<\/h3>\n<p><code>Collections<\/code> \u7c7b\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u9759\u6001\u65b9\u6cd5\u6765\u64cd\u4f5c\u6216\u8f6c\u6362\u96c6\u5408\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684\u65b9\u6cd5\uff1a<\/p>\n<ul>\n<li>\n<p><code>Collections.sort(\u5217\u8868)<\/code>\uff1a\u5bf9\u5217\u8868\u8fdb\u884c\u6392\u5e8f\u3002<\/p>\n<pre><code class=\"language-java\">List<Integer> numbers = Arrays.asList(3, 1, 4, 1, 5, 9);\nCollections.sort(numbers);\n\/\/ numbers \u73b0\u5728\u662f [1, 1, 3, 4, 5, 9]<\/code><\/pre>\n<\/li>\n<li>\n<p><code>Collections.reverse(\u5217\u8868)<\/code>\uff1a\u53cd\u8f6c\u5217\u8868\u4e2d\u5143\u7d20\u7684\u987a\u5e8f\u3002<\/p>\n<pre><code class=\"language-java\">Collections.reverse(numbers);\n\/\/ numbers \u73b0\u5728\u662f [9, 5, 4, 3, 1, 1]<\/code><\/pre>\n<\/li>\n<li>\n<p><code>Collections.shuffle(\u5217\u8868)<\/code>\uff1a\u968f\u673a\u6253\u4e71\u5217\u8868\u4e2d\u5143\u7d20\u7684\u987a\u5e8f\u3002<\/p>\n<pre><code class=\"language-java\">Collections.shuffle(numbers);\n\/\/ numbers \u88ab\u968f\u673a\u6253\u4e71\u987a\u5e8f<\/code><\/pre>\n<\/li>\n<li>\n<p><code>Collections.swap(\u5217\u8868, i, j)<\/code>\uff1a\u4ea4\u6362\u5217\u8868\u4e2d\u4e24\u4e2a\u7d22\u5f15\u4f4d\u7f6e\u7684\u5143\u7d20\u3002<\/p>\n<pre><code class=\"language-java\">Collections.swap(numbers, 0, 1);\n\/\/ numbers \u4e2d\u7d22\u5f15 0 \u548c 1 \u4f4d\u7f6e\u7684\u5143\u7d20\u88ab\u4ea4\u6362<\/code><\/pre>\n<\/li>\n<li>\n<p><code>Collections.max(\u96c6\u5408)<\/code> \/ <code>Collections.min(\u96c6\u5408)<\/code>\uff1a\u8fd4\u56de\u96c6\u5408\u4e2d\u7684\u6700\u5927\/\u6700\u5c0f\u5143\u7d20\u3002<\/p>\n<pre><code class=\"language-java\">int maxNumber = Collections.max(numbers);\nint minNumber = Collections.min(numbers);<\/code><\/pre>\n<\/li>\n<\/ul>\n<p>\u8fd9\u4e9b\u65b9\u6cd5\u63d0\u4f9b\u4e86\u5feb\u901f\u3001\u9ad8\u6548\u7684\u65b9\u5f0f\u6765\u5904\u7406Java\u4e2d\u7684\u6570\u7ec4\u548c\u96c6\u5408\u3002\u5728\u5b9e\u9645\u7f16\u7a0b\u4e2d\uff0c\u5b83\u4eec\u662f\u5904\u7406\u6570\u636e\u7ed3\u6784\u7684\u91cd\u8981\u5de5\u5177\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u603b\u7ed3Java\u4e2dString,Arrays,Collections\u7c7b\u4e2d\u7684\u9759\u6001\u65b9\u6cd5 \u5728Java\u4e2d\uff0cString\u3001Arrays \u548c Co &#8230;<\/p>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[5,9],"class_list":["post-204","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-java","tag-leetcode"],"_links":{"self":[{"href":"https:\/\/wangqianming.top\/index.php\/wp-json\/wp\/v2\/posts\/204","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wangqianming.top\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wangqianming.top\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wangqianming.top\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wangqianming.top\/index.php\/wp-json\/wp\/v2\/comments?post=204"}],"version-history":[{"count":1,"href":"https:\/\/wangqianming.top\/index.php\/wp-json\/wp\/v2\/posts\/204\/revisions"}],"predecessor-version":[{"id":205,"href":"https:\/\/wangqianming.top\/index.php\/wp-json\/wp\/v2\/posts\/204\/revisions\/205"}],"wp:attachment":[{"href":"https:\/\/wangqianming.top\/index.php\/wp-json\/wp\/v2\/media?parent=204"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wangqianming.top\/index.php\/wp-json\/wp\/v2\/categories?post=204"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wangqianming.top\/index.php\/wp-json\/wp\/v2\/tags?post=204"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}