这里是您的起点,使用地图存储计数:
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.Map;
public class TestCount {
public static void main( String[] args ) {
String myString = "abc abc abc abc aaabbbccc 0000 *&% a";
int whiteSpace = 0;
int textual = 0;
int numerical = 0;
int symbols = 0;
char[] data = myString.toCharArray();
Arrays.sort( data );
Map<Character, Integer> countMap = new LinkedHashMap<>();
for ( char c : data ) {
if ( countMap.containsKey( c ) ) {
countMap.put( c, countMap.get( c ) + 1 );
} else {
countMap.put( c, 1 );
}
}
for ( Map.Entry<Character, Integer> e : countMap.entrySet() ) {
char key = e.getKey();
System.out.printf( "%c -> %d occurences\n", e.getKey(), e.getValue() );
if ( ( key >= 'a' && key <= 'z' ) || ( key >= 'A' && key <= 'Z' ) ) {
textual += e.getValue();
} else if ( key >= '0' && key <= '9' ) {
numerical += e.getValue();
} else if ( key == ' ' ) {
whiteSpace += e.getValue();
} else {
symbols += e.getValue();
}
}
System.out.printf( "%d are textual characters\n", textual );
System.out.printf( "%d are numerical characters\n", numerical );
System.out.printf( "%d are whitespace characters\n", whiteSpace );
System.out.printf( "%d are symbol characters\n", symbols );
}
}
编辑:
这是一个简单而肮脏的解决方案,没有使用排序和地图等内置功能。它将字符代码(表示字符的整数)映射到数组位置。由于字符是有序的,数据数组的最后一个字符是代码较大的字符,将是最后一个有效的数组位置。
public class TestCount {
public static void main( String[] args ) {
String myString = "abc abc abc abc aaabbbccc 0000 *&% a";
int whiteSpace = 0;
int textual = 0;
int numerical = 0;
int symbols = 0;
char[] data = myString.toCharArray();
mySort( data );
// a counting array. it will waste a lot of space, but it will work
int[] countArray = new int[ (int) data[data.length-1] + 1 ];
for ( char c : data ) {
countArray[ (int) c ]++;
}
char lastChar = '\0';
for ( char c : data ) {
if ( c != lastChar ) {
System.out.printf( "%c -> %d occurences\n", c, countArray[ (int) c ] );
}
if ( ( c >= 'a' && c <= 'z' ) || ( c >= 'A' && c <= 'Z' ) ) {
textual++;
} else if ( c >= '0' && c <= '9' ) {
numerical++;
} else if ( c == ' ' ) {
whiteSpace++;
} else {
symbols++;
}
lastChar = c;
}
System.out.printf( "%d are textual characters\n", textual );
System.out.printf( "%d are numerical characters\n", numerical );
System.out.printf( "%d are whitespace characters\n", whiteSpace );
System.out.printf( "%d are symbol characters\n", symbols );
}
public static void mySort( char[] array ) {
for ( int i = 0; i < array.length; i++ ) {
for ( int j = 0; j < array.length-1; j++ ) {
if ( array[j] > array[j+1] ) {
char t = array[j];
array[j] = array[j+1];
array[j+1] = t;
}
}
}
}
}
编辑 2: 一种更节省空间的方法:
public class TestCount {
public static void main( String[] args ) {
String myString = "abc abc abc abc aaabbbccc 0000 *&% a";
int whiteSpace = 0;
int textual = 0;
int numerical = 0;
int symbols = 0;
char[] data = myString.toCharArray();
mySort( data );
// a counting array. it will waste a lot of space, but it will work
int[] countArray = new int[ (int) data[data.length-1] + 1 - (int) data[0] ];
for ( char c : data ) {
countArray[ map(data[0], c) ]++;
}
char lastChar = '\0';
for ( char c : data ) {
if ( c != lastChar ) {
System.out.printf( "%c -> %d occurence(s)\n", c, countArray[ map(data[0], c) ] );
}
if ( c >= 'A' && c <= 'z' ) {
textual++;
} else if ( c >= '0' && c <= '9' ) {
numerical++;
} else if ( c == ' ' ) {
whiteSpace++;
} else {
symbols++;
}
lastChar = c;
}
System.out.printf( "%d are textual characters\n", textual );
System.out.printf( "%d are numerical characters\n", numerical );
System.out.printf( "%d are whitespace characters\n", whiteSpace );
System.out.printf( "%d are symbol characters\n", symbols );
}
public static int map( char leftBoundary, char charToMap ) {
return (int) charToMap - (int) leftBoundary;
}
public static void mySort( char[] array ) {
for ( int i = 0; i < array.length; i++ ) {
for ( int j = 0; j < array.length-1; j++ ) {
if ( array[j] > array[j+1] ) {
char t = array[j];
array[j] = array[j+1];
array[j+1] = t;
}
}
}
}
}