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

Java运行出现ArrayIndexOutOfBoundsException异常求助

问题排查:Java数组越界异常(ArrayIndexOutOfBoundsException)

异常原因

你代码里的map数组长度为26,对应小写字母a-z的索引范围0-25,但输入的字符串"Inxee"包含大写字母'I'。计算currChar - 'a'时,大写'I'的ASCII码是73,'a'是97,结果为-24,这是一个负数索引,超出了数组的合法访问范围,直接触发ArrayIndexOutOfBoundsException。

解决方案

方案1:统一字符大小写

把字符串里的字符全部转成小写(或大写)后再处理,确保索引计算合法:

public class removeDuplicate5 {
    public static boolean[] map = new boolean[26];

    public static void removeDuplicates(String str, int idx, String newString) {
        if (idx == str.length()) {
            System.out.println(newString);
            return;
        }
        // 转成小写后处理
        char currChar = Character.toLowerCase(str.charAt(idx));
        if (map[currChar - 'a']) {
            removeDuplicates(str, idx + 1, newString);
        } else {
            newString += currChar;
            map[currChar - 'a'] = true;
            removeDuplicates(str, idx + 1, newString);
        }
    }

    public static void main(String[] args) {
        String company = "Inxee";
        removeDuplicates(company, 0, "");
    }
}

方案2:增加边界校验

只处理小写字母a-z的字符,其他字符直接跳过:

public class removeDuplicate5 {
    public static boolean[] map = new boolean[26];

    public static void removeDuplicates(String str, int idx, String newString) {
        if (idx == str.length()) {
            System.out.println(newString);
            return;
        }
        char currChar = str.charAt(idx);
        // 只处理小写a-z范围的字符
        if (currChar >= 'a' && currChar <= 'z') {
            if (map[currChar - 'a']) {
                removeDuplicates(str, idx + 1, newString);
            } else {
                newString += currChar;
                map[currChar - 'a'] = true;
                removeDuplicates(str, idx + 1, newString);
            }
        } else {
            // 非目标字符直接递归跳过
            removeDuplicates(str, idx + 1, newString);
        }
    }

    public static void main(String[] args) {
        String company = "Inxee";
        removeDuplicates(company, 0, "");
    }
}

额外优化建议

你的map是静态全局变量,多次调用removeDuplicates方法时会保留之前的状态,容易导致错误。可以把map作为方法参数传入,每次调用时创建新的数组:

public class removeDuplicate5 {

    public static void removeDuplicates(String str, int idx, String newString, boolean[] map) {
        if (idx == str.length()) {
            System.out.println(newString);
            return;
        }
        char currChar = Character.toLowerCase(str.charAt(idx));
        if (currChar >= 'a' && currChar <= 'z') {
            if (map[currChar - 'a']) {
                removeDuplicates(str, idx + 1, newString, map);
            } else {
                newString += currChar;
                map[currChar - 'a'] = true;
                removeDuplicates(str, idx + 1, newString, map);
            }
        } else {
            removeDuplicates(str, idx + 1, newString, map);
        }
    }

    public static void main(String[] args) {
        String company = "Inxee";
        // 每次调用创建新的map数组,避免状态污染
        removeDuplicates(company, 0, "", new boolean[26]);
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.12 02:42:54