ArrayList的子列表IndexOutOfBounds异常 [英] Arraylist Sublist IndexOutOfBounds Exception

查看:497
本文介绍了ArrayList的子列表IndexOutOfBounds异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

工作下面code修改HL7消息,并添加一些十六进制字符根据病情,我已经写了。

Working on below code to Modify HL7 messages and add some Hex Characters depending on condition i have written.

复制从文本字符串并将其转换为数组列表,这样我可以通过它遍历找到HL7段的名称。要做到这一点我得到一个子列表出来的ArrayList。但低于code工作时,我收到IndexOutOf越界异常。我知道,子列表搜索,当我搜索超过ArrayList的大小。只需要一个尖检查子表的值不超越ArrayList的大小。

Copying the String from the textbox and converting it to Arraylist so that i can traverse through it to find the names of the HL7 segments. To do so i am getting a sublist out of Arraylist. But when working with below code i am getting IndexOutOf Bounds exception. I know that when searching for Sublist my search is exceeding the size of Arraylist. Just need a tip to check if the Value of Sublist is not going beyond Arraylist Size.

下面是code为相同:

Below is the code for the same :

public String validateMsg(String msg) {
    msg = msg.replaceAll("[^a-zA-Z0-9] && ^[|&^~#.$]", null);

    msg = msg.replaceAll("\\s", "");
    msg = msg.replaceAll("\u000b", "");

    String strArray[] = msg.split("");

    ArrayList<String> msgArray = new ArrayList<String>(
            Arrays.asList(strArray));

    msgArray.add(0, "\u000b");

    for (int i = 0; i + 3 < msgArray.size(); i++) {

        String tmpSeg = "";

        StringBuilder sb = new StringBuilder();

        for (String tmpSeg1 : msgArray.subList(i, i + 3)) {
            sb.append(tmpSeg1).append("");
        }

        tmpSeg = sb.toString();

        if (tmpSeg.equals("EVN") && !msgArray.get(i - 1).contains("\r")
                || tmpSeg.equals("MRG")
                && !msgArray.get(i - 1).contains("\r")
                || tmpSeg.equals("PID")
                && !msgArray.get(i - 1).contains("\r")
                || tmpSeg.equals("PD1")
                && !msgArray.get(i - 1).contains("\r")
                || tmpSeg.equals("PV2")
                && !msgArray.get(i - 1).contains("\r")
                || tmpSeg.equals("OBR")
                && !msgArray.get(i - 1).contains("\r")
                || tmpSeg.equals("ORC")
                && !msgArray.get(i - 1).contains("\r")
                || tmpSeg.equals("OBX")
                && !msgArray.get(i - 1).contains("\r")
                || tmpSeg.equals("SPM")
                && !msgArray.get(i - 1).contains("\r")
                || tmpSeg.equals("ZFM")
                && !msgArray.get(i - 1).contains("\r")
                || tmpSeg.equals("GT1")
                && !msgArray.get(i - 1).contains("\r")
                || tmpSeg.equals("ZEP")
                && !msgArray.get(i - 1).contains("\r")
                || tmpSeg.equals("ZVT")
                && !msgArray.get(i - 1).contains("\r")
                || tmpSeg.equals("AL1")
                && !msgArray.get(i - 1).contains("\r")
                || tmpSeg.equals("FT1")
                && !msgArray.get(i - 1).contains("\r")
                || tmpSeg.equals("IN1")
                && !msgArray.get(i - 1).contains("\r")
                || tmpSeg.equals("IN2")
                && !msgArray.get(i - 1).contains("\r")
                || tmpSeg.equals("IN3")
                && !msgArray.get(i - 1).contains("\r")
                || tmpSeg.equals("NK1")
                && !msgArray.get(i - 1).contains("\r")
                || tmpSeg.equals("ACC")
                && !msgArray.get(i - 1).contains("\r")
                || tmpSeg.equals("PR1")
                && !msgArray.get(i - 1).contains("\r")
                || tmpSeg.equals("UB1")
                && !msgArray.get(i - 1).contains("\r")
                || tmpSeg.equals("UB2")
                && !msgArray.get(i - 1).contains("\r")
                || tmpSeg.equals("PD1")
                && !msgArray.get(i - 1).contains("\r")
                || tmpSeg.equals("RXO")
                && !msgArray.get(i - 1).contains("\r")
                || tmpSeg.equals("RXE")
                && !msgArray.get(i - 1).contains("\r")
                || tmpSeg.equals("RXR")
                && !msgArray.get(i - 1).contains("\r")
                || tmpSeg.equals("RXC")
                && !msgArray.get(i - 1).contains("\r")
                || tmpSeg.equals("RXO")
                && !msgArray.get(i - 1).contains("\r")
                || tmpSeg.equals("ROL")
                && !msgArray.get(i - 1).contains("\r")
                || tmpSeg.equals("DB1")
                && !msgArray.get(i - 1).contains("\r")
                || tmpSeg.equals("DRG")
                && !msgArray.get(i - 1).contains("\r")
                || tmpSeg.equals("PDA")
                && !msgArray.get(i - 1).contains("\r")
                || tmpSeg.equals("NTE")
                && !msgArray.get(i - 1).contains("\r")
                || tmpSeg.equals("ODS")
                && !msgArray.get(i - 1).contains("\r")
                || tmpSeg.equals("ODT")
                && !msgArray.get(i - 1).contains("\r")
                || tmpSeg.equals("SCH")
                && !msgArray.get(i - 1).contains("\r")
                || tmpSeg.equals("ZDS")
                && !msgArray.get(i - 1).contains("\r")
                || tmpSeg.equals("ZIN")
                && !msgArray.get(i - 1).contains("\r")
                || tmpSeg.equals("ZAL")
                && !msgArray.get(i - 1).contains("\r")
                || tmpSeg.equals("NPU")
                && !msgArray.get(i - 1).contains("\r")
                || tmpSeg.equals("ZAB")
                && !msgArray.get(i - 1).contains("\r")
                || tmpSeg.equals("ZAU")
                && !msgArray.get(i - 1).contains("\r")
                || tmpSeg.equals("ZEP")
                && !msgArray.get(i - 1).contains("\r")
                || tmpSeg.equals("ZEV")
                && !msgArray.get(i - 1).contains("\r")
                || tmpSeg.equals("ZRC")
                && !msgArray.get(i - 1).contains("\r")
                || tmpSeg.equals("ZAT")
                && !msgArray.get(i - 1).contains("\r")
                || tmpSeg.equals("ZIV")
                && !msgArray.get(i - 1).contains("\r")
                || tmpSeg.equals("TXA")
                && !msgArray.get(i - 1).contains("\r")
                || tmpSeg.equals("MFI")
                && !msgArray.get(i - 1).contains("\r")
                || tmpSeg.equals("MFE")
                && !msgArray.get(i - 1).contains("\r")
                || tmpSeg.equals("STF")
                && !msgArray.get(i - 1).contains("\r")
                || tmpSeg.equals("PRA")
                && !msgArray.get(i - 1).contains("\r")
                || tmpSeg.equals("PID")
                && !msgArray.get(i - 1).contains("\r")
                || tmpSeg.equals("AIS")
                && !msgArray.get(i - 1).contains("\r")
                || tmpSeg.equals("AIG")
                && !msgArray.get(i - 1).contains("\r")
                || tmpSeg.equals("AIL")
                && !msgArray.get(i - 1).contains("\r")
                || tmpSeg.equals("AIP")
                && !msgArray.get(i - 1).contains("\r")
                || tmpSeg.equals("RRA")
                && !msgArray.get(i - 1).contains("\r")
                || tmpSeg.equals("PV1")
                && !msgArray.get(i - 1).contains("\r")) {
            msgArray.add(i, "\r");
            // break;
        }

    }

    msgArray.add(msgArray.size(), "\r");
    msgArray.add(msgArray.size(), "\u001C\r");

    StringBuilder strBuilder = new StringBuilder();
    for (String str : msgArray) {
        strBuilder.append(str).append("");
    }

    return strBuilder.toString();
}

请帮我一些提示,使我的病情将检查如果我没有渡ArrayList的大小。

Please help me with some tip so that my condition will check if i am not crossing the ArrayList Size.

先谢谢了。

推荐答案

尝试以下code,

   if((i+3)<= msgArray.size()){
    for(String tmpSeg1:msgArray.subList(i, i+3))
        {
            sb.append(tmpSeg1).append("");
        }
    }else{

     System.out.println("Exceeds size");
    }

这篇关于ArrayList的子列表IndexOutOfBounds异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆