方法:采用String的split验证代码如下:
import javautilArrays;
public class TestSplit {
public static void main(String[] args) {
String orignString = new String();
String[] testString = orignStringsplit();
int[] test = { };
//String to int
for (int i = ; i < testStringlength; i++) {
test[i] = IntegerparseInt(testString[i]);
}
//sort
Arrayssort(test);
//asc sort
for (int j = ; j < testlength; j++) {
Systemoutprintln(test[j]);
}
Systemoutprintln(next );
// desc
for (int i = (testlength ); i >= ; i) {
Systemoutprintln(test[i]);
}
}
}
方法:采用StringTokenizer
import javautilArrays;
import javautilStringTokenizer;
public class SplitStringTest {
public static void main(String[] args) {
String s = new String();
int length = slength();
//split s with
StringTokenizer commaToker = new StringTokenizer(s );
String[] result = new String[untTokens()];
int k = ;
while (commaTokerhasMoreTokens()) {
result[k] = commaTokernextToken();
k++;
}
int[] a = new int[resultlength];
for (int i = ; i < resultlength; i++) {
a[i] = IntegerparseInt(result[i]);
}
//sort
Arrayssort(a);
//asc sort
for (int j = ; j < resultlength; j++) {
Systemoutprintln(a[j]);
}
}
}