There are many ways. You don't show an ArrayList in your question. You show a String. If that's the case, RegEx and StringBuilder are the fastest and cleanest algo...
public class SortDemNumsOrAlphas {
public static void main(String[] args) {
String s = "[1, 2, 3, 4, 5, a, b, c]";
String ss = s.replaceAll( "[^A-Z0-9a-z]", "" );
System.out.println(ss);
String nums = ss.replaceAll( "[^0-9]", "" );
String alpha = ss.replaceAll( "[^A-Za-z]", "" );
System.out.println( newString( nums ));
System.out.println( newString( alpha));
}
private static String newString( String s ) {
StringBuilder sb = new StringBuilder();
sb.append( '[' );
for (int i = 0; i < s.length(); i++) {
sb.append( s.charAt( i ))
.append( "," )
.append( " ");
}
// take off last comma
int end = sb.lastIndexOf( "," );
sb.delete(end, sb.length() ).append( ']' );
return sb.toString( );
}
}
// otherwise
The wrapper Character has the following methods() in the API
Character.isLetterOrDigit( c );
Character.isLetter( c );
Character.isDigit( c );
// they all result in a boolean value
// you would use it like so
Character C = s.charAt(1);
if( Character.isDigit( C )) {
}