java - ArrayList ignore null value causing ArrayIndexOutOfBoundsException -
i trying specific data line using ! split. example:
s!arc!256!547291!840.50!9038.00!840.50!9038.00!519.50!9038.00!321.00!0.00!counterclockwise!assembly_top!bat-2-cr1632-t0_r!bt2k!
so when split line using ! , put in array, array should contain 16 data. facing problem when data like
s!line!257!37825 1!3525.00!10720.00!4525.00!10720.00!6.00!!!!!assembly_top!csco-label-asy-73-sn-comb_r!!
and ignore last data null or "". when try take data[15] , problem arrayindexoutofboundsexception keep pop out.below code write:
string[] data = $line_list.get(k).split("!"); if ($line_list.get(k).startswith("s!") && $line_list.get(k).contains("assembly_top")) { $graphic.append(data[15]) .append("!") .append(data[14]) .append("!") .append("top") .append("\n"); }
any way solve problem??
split
eliminates trailing empty strings. therefore, in example length of array 15, data[15]
out of bounds.
0 "s" 1 "line" 2 "257" 3 "37825 1" 4 "3525.00" 5 "10720.00" 6 "4525.00" 7 "10720.00" 8 "6.00" 9 "" 10 "" 11 "" 12 "" 13 "assembly_top" 14 "csco-label-asy-73-sn-comb_r"
you must check data.length > 15
before accessing data[15]
.
here's relevant quote javadoc of split
:
this method works if invoking two-argument split method given expression , limit argument of zero. trailing empty strings therefore not included in resulting array.
Comments
Post a Comment