删除string[]数组一条记录

 

在一些字符串数组中,常会有重复的记录,比如手机号码,我们可以通过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;
}
示例:
String[] mobile=;
mobile=checkArray(mobile);
for(int i=0;i<mobile.length;i++)
System.out.println(mobile[i]);
输出结果为:
13811071503
13811071501
13811071500
2.A,B均为字符串数组,找出在A中存在,而在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;
}
示例:
String[] mobile1=;
String[] mobile2=;
String[] mobile3=compareArray(mobile1,mobile2);
for(int i=0;i<mobile3.length;i++)
System.out.println(mobile[i]);
输出结果:
13811071503
13811071501
存在的问题:
每次都是倒序,可以再对程序稍加改动,变成正序。

3.将一个字符串数组中某一个特定的字符串过滤掉

/** *//**检验一个字符串数组,若包含某一特定的字符串,则将该字符串从数组中删
除,返回剩余的字符串数组
* @param str_array 字符串数组
* @param str_remove 待删除的字符串
* @return 过滤后的字符串
*/
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]);
}
//生成一个新的数组
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;
}
以下是一个简单的员工绩效管理程序的示例代码: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_EMPLOYEE 1000 typedef struct { int id; char name[30]; char department[30]; float score; } Employee; Employee employees[MAX_EMPLOYEE]; Employee sorted_employees[MAX_EMPLOYEE]; int num_employees = 0; void create_employee() { if (num_employees >= MAX_EMPLOYEE) { printf("Cannot add more employees.\n"); return; } printf("Enter employee ID: "); scanf("%d", &employees[num_employees].id); printf("Enter employee name: "); scanf("%s", employees[num_employees].name); printf("Enter employee department: "); scanf("%s", employees[num_employees].department); printf("Enter employee score: "); scanf("%f", &employees[num_employees].score); num_employees++; } void display_employees() { printf("ID\tName\tDepartment\tScore\n"); for (int i = 0; i < num_employees; i++) { printf("%d\t%s\t%s\t\t%.2f\n", employees[i].id, employees[i].name, employees[i].department, employees[i].score); } } void clear_employees() { num_employees = 0; } void save_employees() { FILE *fp; fp = fopen("employees.dat", "wb"); fwrite(employees, sizeof(Employee), num_employees, fp); fclose(fp); } void load_employees() { FILE *fp; fp = fopen("employees.dat", "rb"); if (fp) { num_employees = fread(employees, sizeof(Employee), MAX_EMPLOYEE, fp); fclose(fp); } } void search_employee() { int id; printf("Enter employee ID: "); scanf("%d", &id); for (int i = 0; i < num_employees; i++) { if (employees[i].id == id) { printf("ID\tName\tDepartment\tScore\n"); printf("%d\t%s\t%s\t\t%.2f\n", employees[i].id, employees[i].name, employees[i].department, employees[i].score); return; } } printf("Employee not found.\n"); } void delete_employee() { int id; printf("Enter employee ID: "); scanf("%d", &id); for (int i = 0; i < num_employees; i++) { if (employees[i].id == id) { for (int j = i; j < num_employees - 1; j++) { employees[j] = employees[j+1]; } num_employees--; printf("Employee deleted.\n"); return; } } printf("Employee not found.\n"); } void add_employee() { if (num_employees >= MAX_EMPLOYEE) { printf("Cannot add more employees.\n"); return; } Employee new_employee; printf("Enter employee ID: "); scanf("%d", &new_employee.id); printf("Enter employee name: "); scanf("%s", new_employee.name); printf("Enter employee department: "); scanf("%s", new_employee.department); printf("Enter employee score: "); scanf("%f", &new_employee.score); int i = num_employees; while (i > 0 && new_employee.score > employees[i-1].score) { employees[i] = employees[i-1]; i--; } employees[i] = new_employee; num_employees++; } void sort_employees() { memcpy(sorted_employees, employees, num_employees * sizeof(Employee)); for (int i = 0; i < num_employees - 1; i++) { for (int j = i + 1; j < num_employees; j++) { if (sorted_employees[j].score > sorted_employees[i].score) { Employee temp = sorted_employees[i]; sorted_employees[i] = sorted_employees[j]; sorted_employees[j] = temp; } } } printf("ID\tName\tDepartment\tScore\n"); for (int i = 0; i < num_employees; i++) { printf("%d\t%s\t%s\t\t%.2f\n", sorted_employees[i].id, sorted_employees[i].name, sorted_employees[i].department, sorted_employees[i].score); } } int main() { int choice; do { printf("\nEmployee Performance Management System\n"); printf("1. Create employee\n"); printf("2. Display all employees\n"); printf("3. Clear all employees\n"); printf("4. Save employees to file\n"); printf("5. Load employees from file\n"); printf("6. Search employee\n"); printf("7. Delete employee\n"); printf("8. Add employee\n"); printf("9. Sort employees by score\n"); printf("0. Exit\n"); printf("Enter your choice: "); scanf("%d", &choice); switch (choice) { case 1: create_employee(); break; case 2: display_employees(); break; case 3: clear_employees(); printf("All employees cleared.\n"); break; case 4: save_employees(); printf("Employees saved to file.\n"); break; case 5: load_employees(); printf("Employees loaded from file.\n"); break; case 6: search_employee(); break; case 7: delete_employee(); break; case 8: add_employee(); break; case 9: sort_employees(); break; case 0: printf("Exiting program.\n"); break; default: printf("Invalid choice.\n"); break; } } while (choice != 0); return 0; } ``` 这个程序使用了一个结构体类型 `Employee` 来表示员工绩效记录,包含了员工的编号、姓名、部门和绩效分数。这些记录保存在一个全局的结构体数组 `employees` 中,最多可以保存1000条记录。 程序的主菜单提供了多种操作,包括创建员工绩效记录、显示所有记录、清空所有记录、保存记录到文件、从文件读取记录、查询记录删除记录、添加记录和排序记录。其中,添加记录和排序记录比较特殊,需要在插入新记录和排序后重新调整数组中的元素顺序。 注意,在实际应用中,可能需要增加更多的输入验证和错误处理,以保证程序的健壮性和安全性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值