我已经拆了字符串。可能有1个部分或可能是更多部分。我想获得最多100个第一部分,但很少有可能不存在。当部分没有价值时,我试图调试应用程序时出错。这是我尝试过的,但它不起作用:
String[] a = null;
a = out.split("\\{");
String c1 = "0";String c2 = "0";String c3 = "0";String c4 = "0";String c5 = "0";String c6 = "0";String c7 = "0";String c8 = "0";String c9 = "0";String c10 = "0";
String c11 = "0";String c12 = "0";String c13 = "0";String c14 = "0";String c15 = "0";String c16 = "0";String c17 = "0";String c18 = "0";String c19 = "0";String c20 = "0";
String c21 = "0";String c22 = "0";String c23 = "0";String c24 = "0";String c25 = "0";String c26 = "0";String c27 = "0";String c28 = "0";String c29 = "0";String c30 = "0";
String c31 = "0";String c32 = "0";String c33 = "0";String c34 = "0";String c35 = "0";String c36 = "0";String c37 = "0";String c38 = "0";String c39 = "0";String c40 = "0";
String c41 = "0";String c42 = "0";String c43 = "0";String c44 = "0";String c45 = "0";String c46 = "0";String c47 = "0";String c48 = "0";String c49 = "0";String c50 = "0";
String c51 = "0";String c52 = "0";String c53 = "0";String c54 = "0";String c55 = "0";String c56 = "0";String c57 = "0";String c58 = "0";String c59 = "0";String c60 = "0";
String c61 = "0";String c62 = "0";String c63 = "0";String c64 = "0";String c65 = "0";String c66 = "0";String c67 = "0";String c68 = "0";String c69 = "0";String c70 = "0";
String c71 = "0";String c72 = "0";String c73 = "0";String c74 = "0";String c75 = "0";String c76 = "0";String c77 = "0";String c78 = "0";String c79 = "0";String c80 = "0";
String c81 = "0";String c82 = "0";String c83 = "0";String c84 = "0";String c85 = "0";String c86 = "0";String c87 = "0";String c88 = "0";String c89 = "0";String c90 = "0";
String c91 = "0";String c92 = "0";String c93 = "0";String c94 = "0";String c95 = "0";String c96 = "0";String c97 = "0";String c98 = "0";String c99 = "0";String c100 = "0";
if(!a[2].isEmpty()){
String[] b1 = a[2].split(",");c1 = b1[1].split(":")[1];
}
if(!a[3].isEmpty()){
String[] b2 = a[3].split(",");c2 = b2[1].split(":")[1];
}
if(!a[4].isEmpty()){
String[] b3 = a[4].split(",");c3 = b3[1].split(":")[1];
}
if(!a[5].isEmpty()){
String[] b4 = a[5].split(",");c4 = b4[1].split(":")[1];
}
if(!a[6].isEmpty()){
String[] b5 = a[6].split(",");c5 = b5[1].split(":")[1];
}
if(!a[7].isEmpty()){
String[] b6 = a[7].split(",");c6 = b6[1].split(":")[1];
}
到目前为止,只有[2]和[3]有一些值,而[4]应该是空的。但看起来它并没有......这是一个错误:
java.lang.ArrayIndexOutOfBoundsException: 4
at someapp.stan(sapp.java:86)
at someapp.<init>(sapp.java:417)
at someapp.run(sapp.java:39)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$300(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
这是第39行
frame.getContentPane().add(new TestPane());
第417行(stan(id)是公共字符串,其中是我的分割字符串)
stant = stan(id);
第86行
if(!a[4].isEmpty()){
答案 0 :(得分:1)
您应该检查是否
a.length < 4
现在你已经调用了一个不存在的项目,而是
for(int i=0;i<a.length;i++) // will never get an IndexOutOfBounds
a[i];// <-- do something....
答案 1 :(得分:1)
此处行if(!a[4].isEmpty()){
会引发ArrayIndexOutOfBoundsException: 4
异常,因为索引4
高于length
的{{1}}且没有此索引
因此,您应该始终检查您使用的索引是否低于数组长度。尝试打印数组a
的{{1}}并查看您获得的字符串数。 / p>
您可以使用for循环遍历数组字符串或在给定的if条件中检查它,如下所示:
length