The ind list contains the indices to be removed.
But the people commenting above are right, computationally i.e.
algorithmically viewed, you should use a better data structure
here, rather than ArrayList.
import java.util.ArrayList;
import java.util.List;
public class Test005 {
public static void main(String[] args) {
List<String> collection = new ArrayList<String>();
collection.add("1 w a");
collection.add("2 r a");
collection.add("1 r b");
collection.add("2 r b");
collection.add("3 w a");
List<Integer> ind = new ArrayList<Integer>();
for (int i=0; i<collection.size(); i++){
for (int j=0; j<i; j++){
if (collection.get(j).charAt(0) == collection.get(i).charAt(0)){
ind.add(i);
break;
}
}
}
for (int k=0; k<ind.size(); k++){
collection.remove(ind.get(k).intValue());
}
for (int i=0; i<collection.size(); i++){
System.out.println(collection.get(i));
}
}
}
Map, mapping first chars to Strings although this may be overkill.Setand check the list for the next string's first character at each step. let me know if you are not getting the point clearly.