{"id":65585,"date":"2025-05-03T12:14:25","date_gmt":"2025-05-03T04:14:25","guid":{"rendered":"https:\/\/fwq.ai\/blog\/65585\/"},"modified":"2025-05-03T12:14:25","modified_gmt":"2025-05-03T04:14:25","slug":"java%e6%95%b0%e7%bb%84%e5%af%b9%e9%98%9f%e5%88%97%e6%80%8e%e4%b9%88%e6%93%8d%e4%bd%9c-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/65585\/","title":{"rendered":"java\u6570\u7ec4\u5bf9\u961f\u5217\u600e\u4e48\u64cd\u4f5c"},"content":{"rendered":"<blockquote><p>\n  \u5229\u7528 java \u6570\u7ec4\u5b9e\u73b0\u961f\u5217\uff0c\u9075\u5faa\u5148\u8fdb\u5148\u51fa\u539f\u5219\uff0c\u4e3b\u8981\u64cd\u4f5c\u5305\u62ec\uff1a\u521d\u59cb\u5316\u961f\u5217\u5165\u961f\uff1a\u5224\u65ad\u6ee1\u961f\u5217\uff0c\u6dfb\u52a0\u65b0\u5143\u7d20\u51fa\u961f\uff1a\u5224\u65ad\u7a7a\u961f\u5217\uff0c\u83b7\u53d6\u5e76\u79fb\u9664\u961f\u9996\u5143\u7d20\u68c0\u67e5\u961f\u5217\u72b6\u6001\uff08\u7a7a\/\u6ee1\uff09\u83b7\u53d6\u961f\u5217\u5927\u5c0f\u6e05\u7a7a\u961f\u5217\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/13\/2024111304364192454.jpg\" class=\"aligncenter\" title=\"java\u6570\u7ec4\u5bf9\u961f\u5217\u600e\u4e48\u64cd\u4f5c\u63d2\u56fe\" alt=\"java\u6570\u7ec4\u5bf9\u961f\u5217\u600e\u4e48\u64cd\u4f5c\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5229\u7528 Java \u6570\u7ec4\u5b9e\u73b0\u961f\u5217<\/strong><\/p>\n<p>Java \u6570\u7ec4\u53ef\u4ee5\u7528\u6765\u6a21\u62df\u961f\u5217\u7684\u6570\u636e\u7ed3\u6784\u3002\u961f\u5217\u662f\u4e00\u79cd\u5148\u8fdb\u5148\u51fa\uff08FIFO\uff09\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5176\u4e2d\u6700\u65e9\u6dfb\u52a0\u7684\u5143\u7d20\u5c06\u9996\u5148\u88ab\u79fb\u9664\u3002<\/p>\n<p><strong>\u64cd\u4f5c<\/strong><\/p>\n<p>\u4f7f\u7528\u6570\u7ec4\u5b9e\u73b0\u961f\u5217\u9700\u8981\u4ee5\u4e0b\u64cd\u4f5c\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p><strong>1. \u521d\u59cb\u5316\u961f\u5217<\/strong><\/p>\n<pre>int[] queue = new int[size];\nint head = 0;\nint tail = 0;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u5165\u961f\uff08enqueue\uff09<\/strong><\/p>\n<pre>public void enqueue(int item) {\n    \/\/ \u5224\u65ad\u961f\u5217\u662f\u5426\u5df2\u6ee1\n    if ((tail + 1) % queue.length == head) {\n        throw new IllegalStateException(\"Queue is full\");\n    }\n\n    \/\/ \u5c06\u65b0\u5143\u7d20\u6dfb\u52a0\u5230\u6570\u7ec4\u5c3e\u90e8\n    queue[tail] = item;\n    tail = (tail + 1) % queue.length;\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. \u51fa\u961f\uff08dequeue\uff09<\/strong><\/p>\n<pre>public int dequeue() {\n    \/\/ \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a\n    if (head == tail) {\n        throw new IllegalStateException(\"Queue is empty\");\n    }\n\n    \/\/ \u83b7\u53d6\u961f\u9996\u5143\u7d20\n    int item = queue[head];\n    head = (head + 1) % queue.length;\n    return item;\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>4. \u68c0\u67e5\u961f\u5217\u662f\u5426\u4e3a\u7a7a<\/strong><\/p>\n<pre>public boolean isEmpty() {\n    return head == tail;\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>5. \u68c0\u67e5\u961f\u5217\u662f\u5426\u5df2\u6ee1<\/strong><\/p>\n<pre>public boolean isFull() {\n    return (tail + 1) % queue.length == head;\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>6. \u83b7\u53d6\u961f\u5217\u5927\u5c0f<\/strong><\/p>\n<pre>public int size() {\n    return (tail - head + queue.length) % queue.length;\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>7. \u6e05\u7a7a\u961f\u5217<\/strong><\/p>\n<pre>public void clear() {\n    head = 0;\n    tail = 0;\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4f7f\u7528\u6570\u7ec4\u5b9e\u73b0\u961f\u5217\u65f6\uff0c\u9700\u8981\u6ce8\u610f\u4ee5\u4e0b\u51e0\u70b9\uff1a<\/p>\n<ul>\n<li>\u6570\u7ec4\u5927\u5c0f\u9700\u8981\u9884\u5148\u786e\u5b9a\uff0c\u4e00\u65e6\u786e\u5b9a\u65e0\u6cd5\u52a8\u6001\u6269\u5c55\u3002<\/li>\n<li>\u9700\u8981\u8003\u8651\u961f\u5217\u6ee1\u548c\u7a7a\u7684\u7279\u6b8a\u60c5\u51b5\u3002<\/li>\n<li>\u7531\u4e8e\u4f7f\u7528\u4e86\u6a21\u8fd0\u7b97\uff0c\u961f\u5217\u7684\u5faa\u73af\u7d22\u5f15\u53ef\u80fd\u5bfc\u81f4\u5c3e\u90e8\u6307\u9488\u5c0f\u4e8e\u5934\u90e8\u6307\u9488\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u6570\u7ec4\u5bf9\u961f\u5217\u600e\u4e48\u64cd\u4f5c\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>\u5229\u7528 java \u6570\u7ec4\u5b9e\u73b0\u961f\u5217\uff0c\u9075\u5faa\u5148\u8fdb\u5148\u51fa\u539f\u5219\uff0c\u4e3b\u8981\u64cd\u4f5c\u5305\u62ec\uff1a\u521d\u59cb\u5316\u961f\u5217\u5165\u961f\uff1a\u5224\u65ad\u6ee1\u961f\u5217\uff0c\u6dfb\u52a0\u65b0\u5143\u7d20\u51fa\u961f\uff1a\u5224\u65ad\u7a7a\u961f\u5217\uff0c\u83b7\u53d6\u5e76\u79fb\u9664\u961f\u9996\u5143\u7d20\u68c0\u67e5\u961f\u5217\u72b6\u6001\uff08\u7a7a\/\u6ee1\uff09\u83b7\u53d6\u961f\u5217\u5927\u5c0f\u6e05\u7a7a\u961f\u5217 \u5229\u7528 Java \u6570\u7ec4\u5b9e\u73b0\u961f\u5217 Java \u6570\u7ec4\u53ef\u4ee5\u7528\u6765\u6a21\u62df\u961f\u5217\u7684\u6570\u636e\u7ed3\u6784\u3002\u961f\u5217\u662f\u4e00\u79cd\u5148\u8fdb\u5148\u51fa\uff08FIFO\uff09\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5176\u4e2d\u6700\u65e9\u6dfb\u52a0\u7684\u5143\u7d20\u5c06\u9996\u5148\u88ab\u79fb\u9664\u3002 \u64cd\u4f5c \u4f7f\u7528\u6570\u7ec4\u5b9e\u73b0\u961f\u5217\u9700\u8981\u4ee5\u4e0b\u64cd\u4f5c\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b 1. \u521d\u59cb\u5316\u961f\u5217 int[] queue = new int[size]; int head = 0; int tail = 0; \u767b\u5f55\u540e\u590d\u5236 2. \u5165\u961f\uff08enqueue\uff09 public void enqueue(int item) { \/\/ \u5224\u65ad\u961f\u5217\u662f\u5426\u5df2\u6ee1 if ((tail + 1) % queue.length == head) { throw new IllegalStateException(&#8220;Queue is full&#8221;); } \/\/ \u5c06\u65b0\u5143\u7d20\u6dfb\u52a0\u5230\u6570\u7ec4\u5c3e\u90e8 queue[tail] = [&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-65585","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/65585","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=65585"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/65585\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=65585"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=65585"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=65585"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}