I have a sorted ArrayList A based on substring(1,4). I want to add all the Strings with the same substring(1,4) to a new ArrayList.
ArrayList A: x111abc, x111acb, x111bca, x222abc, x222cba, x333abc, x333bca, x444abc
So I want x111abc, x111acb, x111bca all in one ArrayList, x222abc, x222cba in a different ArrayList, x333abc, x333bca in a different ArrayList, and x444abc in a different ArrayList.
I've tried this and a while loop (instead of the if statement) similar to this, but I know that it's going to create an error once I get to the last item in A. I feel like there has to be an easier way, but I can't figure it out.
for (int i = 0; i < A.size(); i++) {
if (A.get(i).substring(1, 4).equals(A.get(i).substring(1, 4))) {
//add it to an ArrayList
}
}