{"id":66866,"date":"2025-05-03T08:39:17","date_gmt":"2025-05-03T00:39:17","guid":{"rendered":"https:\/\/fwq.ai\/blog\/66866\/"},"modified":"2025-05-03T08:39:17","modified_gmt":"2025-05-03T00:39:17","slug":"java%e6%80%8e%e4%b9%88%e8%ae%a9%e4%ba%8c%e7%bb%b4%e6%95%b0%e7%bb%84%e5%af%b9%e9%bd%90%e8%be%93%e5%87%ba-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/66866\/","title":{"rendered":"java\u600e\u4e48\u8ba9\u4e8c\u7ef4\u6570\u7ec4\u5bf9\u9f50\u8f93\u51fa"},"content":{"rendered":"<blockquote><p>\n  \u901a\u8fc7 java \u4e2d\u7684 printf \u65b9\u6cd5\uff0c\u53ef\u4ee5\u4f7f\u7528 %n$ \u8bed\u6cd5\u5bf9\u9f50\u4e8c\u7ef4\u6570\u7ec4\u5143\u7d20\uff1a\u786e\u5b9a\u6bcf\u5217\u7684\u6700\u5927\u503c\u957f\u5ea6\u3002\u521b\u5efa\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\uff0c\u6307\u5b9a\u5217\u5bbd\u548c\u5bf9\u9f50\u65b9\u5f0f\u3002\u904d\u5386\u6570\u7ec4\uff0c\u4f7f\u7528 printf \u65b9\u6cd5\u548c\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u5bf9\u9f50\u5143\u7d20\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/17\/2024111713093245660.jpg\" class=\"aligncenter\" title=\"java\u600e\u4e48\u8ba9\u4e8c\u7ef4\u6570\u7ec4\u5bf9\u9f50\u8f93\u51fa\u63d2\u56fe\" alt=\"java\u600e\u4e48\u8ba9\u4e8c\u7ef4\u6570\u7ec4\u5bf9\u9f50\u8f93\u51fa\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u8ba9 Java \u4e2d\u7684\u4e8c\u7ef4\u6570\u7ec4\u5bf9\u9f50\u8f93\u51fa<\/strong><\/p>\n<p>\u4f7f\u7528 Java \u4e2d\u7684 printf \u65b9\u6cd5\u53ef\u4ee5\u5bf9\u9f50\u4e8c\u7ef4\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\uff0c\u4ee5\u5b9e\u73b0\u6574\u9f50\u7684\u8f93\u51fa\u3002printf \u65b9\u6cd5\u7684\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u652f\u6301 %n$ \u8bed\u6cd5\uff0c\u5176\u4e2d n \u6307\u5b9a\u8981\u5bf9\u9f50\u7684\u5217\u3002<\/p>\n<p><strong>\u6b65\u9aa4\uff1a<\/strong><\/p>\n<ol>\n<li> <strong>\u786e\u5b9a\u6700\u5927\u5217\u5bbd\uff1a<\/strong>\u9996\u5148\uff0c\u904d\u5386\u6570\u7ec4\u4ee5\u786e\u5b9a\u6bcf\u5217\u7684\u6700\u5927\u503c\u957f\u5ea6\u3002<\/li>\n<li>\n<p><strong>\u521b\u5efa\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\uff1a<\/strong>\u4f7f\u7528\u6700\u5927\u5217\u5bbd\u521b\u5efa\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\uff0c\u6307\u5b9a\u6bcf\u5217\u7684\u5bbd\u5ea6\u548c\u5bf9\u9f50\u65b9\u5f0f\u3002\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u5e94\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>\"%s%-\" + maxLength + \"s\"<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5176\u4e2d\uff1a<\/p>\n<ul>\n<li>%s \u8868\u793a\u503c\u7684\u5360\u4f4d\u7b26\u3002<\/li>\n<li>maxLength \u662f\u6700\u5927\u5217\u5bbd\u3002<\/li>\n<li>&#8211; \u8868\u793a\u5de6\u5bf9\u9f50\u3002<\/li>\n<\/ul>\n<\/li>\n<li> <strong>\u4f7f\u7528 printf \u65b9\u6cd5\uff1a<\/strong>\u904d\u5386\u6570\u7ec4\uff0c\u5e76\u4f7f\u7528 printf \u65b9\u6cd5\u548c\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u5bf9\u6bcf\u5217\u5143\u7d20\u8fdb\u884c\u683c\u5f0f\u5316\u548c\u5bf9\u9f50\u3002<\/li>\n<\/ol>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>import java.util.Arrays;\n\npublic class Align2DArray {\n\n    public static void main(String[] args) {\n        String[][] array = {\n            {\"John\", \"Doe\"},\n            {\"Jane\", \"Doe\"},\n            {\"Bob\", \"Smith\"},\n            {\"Alice\", \"Johnson\"}\n        };\n\n        \/\/ \u786e\u5b9a\u6700\u5927\u5217\u5bbd\n        int maxLength = Arrays.stream(array)\n            .mapToInt(row -&gt; row[0].length())\n            .max()\n            .getAsInt();\n\n        \/\/ \u521b\u5efa\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\n        String format = \"%s%-\" + maxLength + \"s\";\n\n        \/\/ \u5bf9\u9f50\u8f93\u51fa\n        for (String[] row : array) {\n            System.out.printf(format, row[0], row[1]);\n            System.out.println();\n        }\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n<pre>John   Doe\nJane   Doe\nBob    Smith\nAlice  Johnson<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u600e\u4e48\u8ba9\u4e8c\u7ef4\u6570\u7ec4\u5bf9\u9f50\u8f93\u51fa\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8IDCBABY\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u901a\u8fc7 java \u4e2d\u7684 printf \u65b9\u6cd5\uff0c\u53ef\u4ee5\u4f7f\u7528 %n$ \u8bed\u6cd5\u5bf9\u9f50\u4e8c\u7ef4\u6570\u7ec4\u5143\u7d20\uff1a\u786e\u5b9a\u6bcf\u5217\u7684\u6700\u5927\u503c\u957f\u5ea6\u3002\u521b\u5efa\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\uff0c\u6307\u5b9a\u5217\u5bbd\u548c\u5bf9\u9f50\u65b9\u5f0f\u3002\u904d\u5386\u6570\u7ec4\uff0c\u4f7f\u7528 printf \u65b9\u6cd5\u548c\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u5bf9\u9f50\u5143\u7d20\u3002 \u5982\u4f55\u8ba9 Java \u4e2d\u7684\u4e8c\u7ef4\u6570\u7ec4\u5bf9\u9f50\u8f93\u51fa \u4f7f\u7528 Java \u4e2d\u7684 printf \u65b9\u6cd5\u53ef\u4ee5\u5bf9\u9f50\u4e8c\u7ef4\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\uff0c\u4ee5\u5b9e\u73b0\u6574\u9f50\u7684\u8f93\u51fa\u3002printf \u65b9\u6cd5\u7684\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u652f\u6301 %n$ \u8bed\u6cd5\uff0c\u5176\u4e2d n \u6307\u5b9a\u8981\u5bf9\u9f50\u7684\u5217\u3002 \u6b65\u9aa4\uff1a \u786e\u5b9a\u6700\u5927\u5217\u5bbd\uff1a\u9996\u5148\uff0c\u904d\u5386\u6570\u7ec4\u4ee5\u786e\u5b9a\u6bcf\u5217\u7684\u6700\u5927\u503c\u957f\u5ea6\u3002 \u521b\u5efa\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\uff1a\u4f7f\u7528\u6700\u5927\u5217\u5bbd\u521b\u5efa\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\uff0c\u6307\u5b9a\u6bcf\u5217\u7684\u5bbd\u5ea6\u548c\u5bf9\u9f50\u65b9\u5f0f\u3002\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u5e94\u5982\u4e0b\u6240\u793a\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b &#8220;%s%-&#8221; + maxLength + &#8220;s&#8221; \u767b\u5f55\u540e\u590d\u5236 \u5176\u4e2d\uff1a %s \u8868\u793a\u503c\u7684\u5360\u4f4d\u7b26\u3002 maxLength \u662f\u6700\u5927\u5217\u5bbd\u3002 &#8211; \u8868\u793a\u5de6\u5bf9\u9f50\u3002 \u4f7f\u7528 printf \u65b9\u6cd5\uff1a\u904d\u5386\u6570\u7ec4\uff0c\u5e76\u4f7f\u7528 printf \u65b9\u6cd5\u548c\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u5bf9\u6bcf\u5217\u5143\u7d20\u8fdb\u884c\u683c\u5f0f\u5316\u548c\u5bf9\u9f50\u3002 \u793a\u4f8b\uff1a import java.util.Arrays; public class Align2DArray { public static void main(String[] [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-66866","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/66866","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=66866"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/66866\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=66866"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=66866"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=66866"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}