{"record":{"id":"ac57dd02488fb09c","repo":"krahets/hello-algo","slug":"error-ac57dd","errorCode":null,"errorMessage":"キューが空です","messagePattern":"キューが空です","errorType":"exception","errorClass":"IndexError","httpStatus":null,"severity":"error","filePath":"ja/codes/python/chapter_stack_and_queue/array_queue.py","lineNumber":51,"sourceCode":"        # 末尾ポインタを計算し、末尾インデックス + 1 を指す\n        # 剰余演算により、rear が配列末尾を越えた後に先頭へ戻るようにする\n        rear: int = (self._front + self._size) % self.capacity()\n        # num をキュー末尾に追加\n        self._nums[rear] = num\n        self._size += 1\n\n    def pop(self) -> int:\n        \"\"\"デキュー\"\"\"\n        num: int = self.peek()\n        # 先頭ポインタを1つ後ろへ進め、末尾を越えたら配列先頭に戻す\n        self._front = (self._front + 1) % self.capacity()\n        self._size -= 1\n        return num\n\n    def peek(self) -> int:\n        \"\"\"キュー先頭の要素にアクセス\"\"\"\n        if self.is_empty():\n            raise IndexError(\"キューが空です\")\n        return self._nums[self._front]\n\n    def to_list(self) -> list[int]:\n        \"\"\"表示用のリストを返す\"\"\"\n        res = [0] * self.size()\n        j: int = self._front\n        for i in range(self.size()):\n            res[i] = self._nums[(j % self.capacity())]\n            j += 1\n        return res\n\n\n\"\"\"Driver Code\"\"\"\nif __name__ == \"__main__\":\n    # キューを初期化\n    queue = ArrayQueue(10)\n\n    # 要素をエンキュー","sourceCodeStart":33,"sourceCodeEnd":69,"githubUrl":"https://github.com/krahets/hello-algo/blob/69932aed1891a7b7f6a0de88cd116d3fe13e7032/ja/codes/python/chapter_stack_and_queue/array_queue.py#L33-L69","documentation":"This IndexError (Japanese: 'キューが空です' = 'queue is empty') is raised by ArrayQueue.peek() (array_queue.py:51) when _size == 0. peek() returns _nums[_front]; pop() calls peek() first, so the same exception propagates through pop() on an empty queue. The circular-array layout means _front alone does not indicate a valid element — only _size > 0 does.","triggerScenarios":"Calling peek() or pop() on a freshly constructed ArrayQueue. Calling pop() more times than push(). The constructor sets _size = 0 and _front = 0, so any immediate peek/pop raises.","commonSituations":"Consuming faster than producing. Looping a fixed number of pops based on capacity rather than size(). Forgetting that pop() delegates to peek() and will raise the same IndexError.","solutions":["Guard with `if not queue.is_empty(): queue.peek()`.","Drain with `while not queue.is_empty(): queue.pop()`.","Base pop-count on queue.size(), not capacity or a stale length.","In producer/consumer code, check is_empty() before attempting to consume."],"exampleFix":"// before\nval = queue.pop()  # IndexError on empty\n\n// after\nval = queue.pop() if not queue.is_empty() else None","handlingStrategy":"validation","validationCode":"if not queue.is_empty():\n    val = queue.peek()\n# pop() calls peek() first — guard pop the same way","typeGuard":"def queue_has_front(q: ArrayQueue) -> bool:\n    return not q.is_empty()","tryCatchPattern":"try:\n    val = queue.pop()\nexcept IndexError:\n    val = None","preventionTips":["Check is_empty() before peek() or pop() (pop delegates to peek).","Base consume counts on queue.size(), not capacity.","Drain with `while not queue.is_empty():`.","In producer/consumer flows, gate consumption on is_empty()."],"tags":["queue","python","index-error","empty","circular-array"],"backgroundTag":null,"analyzedSha":"69932aed1891a7b7f6a0de88cd116d3fe13e7032","analyzedAt":"2026-08-13T23:02:37.581Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}