1. In some string arrays, there are often duplicate records, such as cell phone numbers, which we can filter through Hashtable public string[] Checkarray (string[] str) ... {
Hashtable<string, string> hash=new hashtable<string, string> ();
for (int i=0;i<str.length;i++) ... {
if (!hash.containskey (Str[i]))
Hash.put (Str[i], str[i]);
}
Enumeration Enumeration=hash.keys ();
String[] Str_new=new string[hash.size ()];
int i=0;
while (Enumeration.hasmoreelements ()) ... {
Str_new[i]=enumeration.nextelement (). toString ();
i++;
}
return str_new;
}
Example:
String[] mobile={"13811071500", "13811071500", "13811071501", "13811071503", "13811071501"};
Mobile=checkarray (mobile);
for (int i=0;i<mobile.length;i++)
System.out.println (Mobile[i]);
The output results are:
13811071503
13811071501
13811071500
2.a,b are all array of strings to find strings that exist in A and that do not exist in B
Public string[] Comparearray (string[] a,string[] B) {
Hashtable<string, string> hash=new hashtable<string, string> ();
Hashtable<string, string> hash_new=new hashtable<string, string> ();
for (int i=0;i<b.length;i++)
Hash.put (B[i], b[i]);
for (int i=0;i<a.length;i++) {
if (!hash.containskey (A[i]))
Hash_new.put (A[i], a[i]);
}
String[] C=new string[hash_new.size ()];
int i=0;
Enumeration Enumeration=hash_new.keys ();
while (Enumeration.hasmoreelements ()) {
C[i]=enumeration.nextelement (). toString ();
i++;
}
return C;
}
Example:
String[] mobile1={"13811071500", "13811071501", "13811071502", "13811071503", "13811071504"};
String[] mobile2={"13811071500", "13811071505", "13811071502", "13811071506", "13811071504"};
String[] Mobile3=comparearray (mobile1,mobile2);
for (int i=0;i<mobile3.length;i++)
System.out.println (Mobile[i]);
Output results:
13811071503
13811071501
The problems that exist:
Each time is in reverse order, you can change the program slightly, into a positive sequence.
3. Filter out a particular string in an array of strings
/** *//** examines an array of strings and, if it contains a particular string, deletes the string from the array
In addition, returns the remaining array of strings
* @param str_array string array
* @param str_remove The string to be deleted
* @return Filtered string
*/
Public string[] Removestrfromarray (string[] str_array,string
Str_remove) ... {
Hashtable<string, string> hash=new hashtable<string, string> ();
for (int i=0;i<str_array.length;i++) ... {
if (!str_array[i].equals (Str_remove))
Hash.put (Str_array[i], str_array[i]);
}
To generate a new array
String[] Str_new=new string[hash.size ()];
int i=0;
Enumeration Enumeration=hash.keys ();
while (Enumeration.hasmoreelements ()) ... {
Str_new[i]=enumeration.nextelement (). toString ();
i++;
}
return str_new;
}