StringTokenizer
StringTokenizer st = new StringTokenizer("persona12,british", ',");
println(st.nextToken());
StringTokenizer st = new StringTokenizer("persona12,british", ',");
println(st.nextToken());
2. split the Strings into two parts, person, country, use the country as a key to add the person to the Vector
3. iterate through the keys (countries) of the HashTable, write out the nameshere is a sample implementation (TestPerson.java):import java.util.*;public class TestPerson
{
public static void main(String[] args)
{
String[] people = {"person1,chinese", "persona12,british","persona113,american","person2224,chinese"}; Hashtable ht = new Hashtable();
String person, country;
int i;
Vector v; for (i=0; i < people.length;i++)
{
int n=people[i].indexOf(',');
if (n >=0)
{
person = people[i].substring(0,n);
country = people[i].substring(n+1);
if (!ht.containsKey(country))
{
v = new Vector();
ht.put(country,v);
} v = (Vector) ht.get(country);
v.add(person);
}
} for (Enumeration e = ht.keys() ; e.hasMoreElements() ;)
{
country = (String)e.nextElement(); System.out.print("The following people are " + country + ":"); v = (Vector)ht.get(country);
for (i=0; i < v.size(); i++)
System.out.print((i > 0? ",":"") + v.get(i)); System.out.println();
}
}
}
然后找","之后的子串 进行比较