{"id":574,"date":"2022-09-27T22:15:52","date_gmt":"2022-09-27T14:15:52","guid":{"rendered":"https:\/\/qaqaq.top\/?p=574"},"modified":"2022-11-27T12:39:59","modified_gmt":"2022-11-27T04:39:59","slug":"%e9%9d%a2%e5%90%91%e5%af%b9%e8%b1%a1%e4%b8%8a-%e8%87%aa%e5%ae%9a%e4%b9%89%e6%95%b0%e7%bb%84%e7%9a%84%e5%b7%a5%e5%85%b7%e7%b1%bb","status":"publish","type":"post","link":"https:\/\/qaqaq.top\/?p=574","title":{"rendered":"\u9762\u5411\u5bf9\u8c61(\u4e0a)-\u81ea\u5b9a\u4e49\u6570\u7ec4\u7684\u5de5\u5177\u7c7b"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>package top.qaqaq.java.P204;\r\n\/*\r\n * \u81ea\u5b9a\u4e49\u6570\u7ec4\u7684\u5de5\u5177\u7c7b\r\n * \r\n * \r\n * \r\n * \r\n *\/\r\npublic class ArrayUtil {\r\n\t\r\n\t\/\/\u6c42\u6570\u7ec4\u7684\u6700\u5927\u503c\r\n\tpublic int getMax(int&#91;] arr) {\r\n\t\tint maxValue = arr&#91;0];\r\n\t\tfor(int i = 1; i &lt; arr.length; i++) {\r\n\t\t\tif(maxValue &lt; arr&#91;i]) {\r\n\t\t\t\tmaxValue = arr&#91;i];\r\n\t\t\t}\r\n\t\t}\r\n\t\treturn maxValue;\r\n\t}\r\n\t\r\n\t\/\/\u6c42\u6570\u7ec4\u7684\u6700\u5c0f\u503c\r\n\tpublic int getMin(int&#91;] arr) {\r\n\t\tint minValue = arr&#91;0];\r\n\t\tfor(int i = 1; i &lt; arr.length; i++) {\r\n\t\t\tif(minValue > arr&#91;i]) {\r\n\t\t\t\tminValue = arr&#91;i];\r\n\t\t\t}\r\n\t\t}\r\n\t\treturn minValue;\r\n\t}\r\n\t\r\n\t\/\/\u6c42\u6570\u7ec4\u7684\u603b\u548c\r\n\tpublic int getSum(int&#91;] arr) {\r\n\t\tint sum = 0;\r\n\t\tfor(int i = 0; i &lt; arr.length; i++) {\r\n\t\t\tsum += arr&#91;i];\r\n\t\t}\r\n\t\treturn sum;\r\n\t}\r\n\t\r\n\t\/\/\u6c42\u6570\u7ec4\u7684\u5e73\u5747\u503c\r\n\tpublic int getAvg(int&#91;] arr) {\r\n\t\t\r\n\t\treturn getSum(arr) \/ arr.length;\r\n\t}\r\n\t\r\n\t\r\n\t\/\/\u53cd\u8f6c\u6570\u7ec4\r\n\tpublic void reverse(int&#91;] arr) {\r\n\t\tfor(int i = 0; i &lt; arr.length \/ 2; i++) {\r\n\t\t\tint temp = arr&#91;i];\r\n\t\t\tarr&#91;i] = arr&#91;arr.length - i - 1];\r\n\t\t\tarr&#91;arr.length - i - 1] = temp;\r\n\t\t}\r\n\t\t\r\n\t}\r\n\t\r\n\t\/\/\u590d\u5236\u6570\u7ec4\r\n\tpublic int&#91;] copy(int&#91;] arr) {\r\n\t\tint&#91;] arr1 = new int&#91;arr.length];\r\n\t\tfor(int i = 0; i &lt; arr.length; i++) {\r\n\t\t\tarr1&#91;i] =  arr&#91;i];\r\n\t\t}\r\n\t\treturn arr1;\r\n\t}\r\n\t\r\n\t\/\/\u6570\u7ec4\u6392\u5e8f\r\n\tpublic void sort(int&#91;] arr) {\r\n\t\tfor(int i = 0; i &lt; arr.length - 1; i++) {\r\n\t\t\tfor(int j = 0; j &lt; arr.length - 1 - i; j++) {\r\n\t\t\t\tif(arr&#91;j] > arr&#91;j + 1]) {\r\n\t\t\t\t\tint temp = arr&#91;j];\r\n\t\t\t\t\tarr&#91;j] = arr&#91;j + 1];\r\n\t\t\t\t\tarr&#91;j + 1] = temp;\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\t\r\n\t\/\/\u904d\u5386\u6570\u7ec4\r\n\tpublic void print(int&#91;] arr) {\r\n\t\tfor(int i = 0; i &lt; arr.length; i++) {\r\n\t\t\tSystem.out.print(arr&#91;i] + \"\\t\");\r\n\t\t}\r\n\t\tSystem.out.println();\r\n\t}\r\n\t\r\n\t\/\/\u67e5\u627e\u6307\u5b9a\u5143\u7d20\r\n\tpublic int getIndex(int&#91;] arr,int dest) {\r\n\t\t\/\/\u7ebf\u6027\u67e5\u627e\uff1a\r\n\t\tfor(int i = 0; i &lt; arr.length; i++) {\r\n\t\t\tif(dest == arr&#91;i]) {\r\n\t\t\t\treturn i;\r\n\t\t\t}\r\n\t\t}\r\n\t\t\r\n\t\treturn -1;\/\/\u8fd4\u56de\u4e00\u4e2a\u8d1f\u6570\uff0c\u8868\u793a\u6ca1\u6709\u627e\u5230\r\n\t}\r\n\t\r\n}\r\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>package top.qaqaq.java.P204;\r\n\r\npublic class ArrayUtilTest {\r\n\tpublic static void main(String&#91;] args) {\r\n\t\t\r\n\t\tArrayUtil util = new ArrayUtil();\r\n\t\tint&#91;] arr = new int&#91;] {32,34,32,5,3,54,654,-98,0,-53,5};\r\n\t\tint max = util.getMax(arr);\r\n\t\tSystem.out.println(\"\u6700\u5927\u503c\u4e3a\uff1a\" + max);\r\n\t\t\r\n\/\/\t\tSystem.out.println(\"\u6392\u5e8f\u524d\uff1a\");\r\n\/\/\t\tutil.print(arr);\r\n\/\/\t\t\r\n\/\/\t\tutil.sort(arr);\r\n\/\/\t\t\r\n\/\/\t\tSystem.out.println(\"\u6392\u5e8f\u540e\uff1a\");\r\n\/\/\t\tutil.print(arr);\r\n\/\/\t\t\r\n\/\/\t\tSystem.out.println(\"\u67e5\u627e\uff1a\");\r\n\t\tint index = util.getIndex(arr, 5);\r\n\t\tif(index >= 0) {\r\n\t\t\tSystem.out.println(\"\u627e\u5230\u4e86\uff0c\u7d22\u5f15\u5730\u5740\u4e3a\uff1a\" + index);\r\n\t\t}else {\r\n\t\t\tSystem.out.println(\"\u672a\u627e\u5230\");\r\n\t\t}\r\n\t}\r\n\r\n}\r\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[46],"class_list":["post-574","post","type-post","status-publish","format-standard","hentry","category-java-code","tag-java"],"_links":{"self":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/574"}],"collection":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=574"}],"version-history":[{"count":1,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/574\/revisions"}],"predecessor-version":[{"id":575,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/574\/revisions\/575"}],"wp:attachment":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=574"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=574"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=574"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}