问题
Working on below code to Modify HL7 messages and add some Hex Characters depending on condition i have written.
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.
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();
}
Please help me with some tip so that my condition will check if i am not crossing the ArrayList Size.
Thanks in advance.
回答1:
try following code,
if((i+3)<= msgArray.size()){
for(String tmpSeg1:msgArray.subList(i, i+3))
{
sb.append(tmpSeg1).append("");
}
}else{
System.out.println("Exceeds size");
}
来源:https://stackoverflow.com/questions/34787347/arraylist-sublist-indexoutofbounds-exception