You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

计算机课实验中ArrayList触发java.lang.IndexOutOfBoundsException的解决求助

Fixing java.lang.IndexOutOfBoundsException in Hand.addCard() Method

Got it, let's break down why you're hitting this exception and fix your code to meet the lab requirements step by step.

Why the IndexOutOfBoundsException occurs

When you first call addCard (with the Card(4)), your myHand ArrayList is empty (size = 0). Your while loop uses the condition i <= myHand.size()—so 0 <= 0 evaluates to true. Then you call getCardValue(i), which tries to run myHand.get(0)—but since the list has no elements yet, index 0 doesn't exist, hence the exception.

Other logic flaws in your addCard method

Beyond the exception, your code doesn't align with the lab rules and has extra bugs:

  • Incorrect loop condition: Using i <= myHand.size() will always trigger an out-of-bounds error when i equals the list size (since list indices range from 0 to size-1).
  • Misaligned insertion logic: The lab asks to find the first Card with a pip value strictly less than c's value, then insert c at that index. Your check c.getValue() >= getCardValue(i) doesn't match this requirement, and you don't break the loop after inserting—this would cause infinite loops or duplicate card inserts.
  • No empty list handling: You need to account for adding the first card without entering the loop.

Fixed Code

Updated getCardValue (with safety check)

While your original getCardValue works for valid indices, adding a check prevents accidental out-of-bounds errors from invalid input:

public int getCardValue(int n) {
    if (n < 0 || n >= myHand.size()) {
        throw new IllegalArgumentException("Index " + n + " is out of bounds. Hand size is " + myHand.size());
    }
    return myHand.get(n).getValue();
}

Corrected addCard method

This version follows the lab requirements exactly and fixes the exception:

public void addCard(Card c) {
    int i = 0;
    // Find the first index where the existing card's value is strictly less than c's value
    while (i < myHand.size()) {
        if (myHand.get(i).getValue() < c.getValue()) {
            break; // Exit loop once we find the insertion point
        }
        i++;
    }
    // Insert the new card at the found index (or end if all existing cards are larger)
    myHand.add(i, c);
}

How this works with your test case

Let's walk through your test scenario to confirm it matches the expected output:

  1. Adding Card(4): myHand is empty, loop doesn't run—card is added at index 0 → [4]
  2. Adding Card(10): Loop checks index 0 (4 < 10), breaks, inserts at 0 → [10,4]
  3. Adding Card(12): Loop checks index 0 (10 <12), breaks, inserts at 0 → [12,10,4]
  4. Adding Card(14): Loop checks index 0 (12 <14), breaks, inserts at 0 → [14,12,10,4]
  5. Adding Card(2): Loop runs through all elements (14,12,10,4 are all ≥2), i reaches 4 (equal to list size), inserts at end → [14,12,10,4,2]

When you call h.getCardValue(3), it returns the value at index 3 (4), which matches your expected output.

内容的提问来源于stack exchange,提问作者Marcellus McDuff

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 04:49:14