java - How to avoid index out p bound exception from arraylist -
i trying substring of arraylist ,but when list empty getting index out of bound exception code
if(interviewtimingtofrom1.size()>0) { (int = 0; < interviewtimingtofrom1.size(); i++) { string str = interviewtimingtofrom1.get(i).substring(0, interviewtimingtofrom1.get(i).length() - 3); subinterviewtiming1.add(str); } } if (interviewtimingtofrom2.size()>0) { (int = 0; < interviewtimingtofrom2.size(); i++) { string str = interviewtimingtofrom2.get(i).substring(0, interviewtimingtofrom2.get(i).length() - 3); subinterviewtiming2.add(str); } }
here when interviewtimingtofrom2
, interviewtimingtofrom1
empty getting index out of bound how solve
try this, may avoid exception.
string str = interviewtimingtofrom2.get(i).substring(0, interviewtimingtofrom2.get(i).length() > 3 ? interviewtimingtofrom2.get(i).length() - 3 : interviewtimingtofrom2.get(i).length());
Comments
Post a Comment