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

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -