.分析以下需求,并用代码实现 1.定义List集合,存入多个字符串 2.删除集合元素字符串中包含0-9数字的字符串 只要字符串中包含0-9中的任意一个数字就需

public class MyText2 {


public static void main(String[] args) {
/*
* 2.分析以下需求,并用代码实现 1.定义List集合,存入多个字符串
*  2.删除集合元素字符串中包含0-9数字的字符串
* (只要字符串中包含0-9中的任意一个数字就需要删除此整个字符串) 
* 3.然后利用迭代器遍历集合元素并输出
*/
//需要一个字符串集合
List<String> strList = new ArrayList<String>();
strList.add("aaa");
strList.add("ccc99");
strList.add("cccer99");
strList.add("ccccsd99");
strList.add("ccasad99");
strList.add("9cads2sa23dsa");
strList.add("ccc32csd99");
strList.add("cc1asad99");
strList.add("cad3a23dsa");
//遍历集合中的元素
for (int i = 0; i < strList.size(); i++) {
//每个元素转换成字符数组
char[] ch = strList.get(i).toCharArray();
//遍历字符数组
for (int j = 0; j < ch.length; j++) {
//判断是否包含
if (ch[j] >= '0' && ch[j] <= '9') {
//进行删除
strList.remove(i);
//改变集合的索引
i--; 
break;//结束本次循环
}
}
}
//迭代
ListIterator<String> listString = strList.listIterator();
//遍历输出
while (listString.hasNext()) {
System.out.println(listString.next());
}
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
好的,我可以回答这个问题。 代码实现如下: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_LEN 100 int main() { char str[MAX_LEN]; int i; List *list = (List*)malloc(sizeof(List)); list->head = NULL; list->tail = NULL; // 存入多个字符串 while (fgets(str, MAX_LEN, stdin) != NULL) { str[strlen(str) - 1] = '\'; // 去掉换行符 Node *node = (Node*)malloc(sizeof(Node)); node->data = (char*)malloc(strlen(str) + 1); strcpy(node->data, str); node->next = NULL; if (list->head == NULL) { list->head = node; list->tail = node; } else { list->tail->next = node; list->tail = node; } } // 删除包含z或m的字符串 Node *prev = NULL; Node *curr = list->head; while (curr != NULL) { int found = ; for (i = ; i < strlen(curr->data); i++) { if (curr->data[i] == 'z' || curr->data[i] == 'm') { found = 1; break; } } if (found) { if (prev == NULL) { list->head = curr->next; } else { prev->next = curr->next; } if (curr == list->tail) { list->tail = prev; } free(curr->data); free(curr); curr = prev == NULL ? list->head : prev->next; } else { prev = curr; curr = curr->next; } } // 遍历集合元素并输出 Node *node = list->head; while (node != NULL) { printf("%s\n", node->data); node = node->next; } // 释放内存 node = list->head; while (node != NULL) { Node *next = node->next; free(node->data); free(node); node = next; } free(list); return ; } ``` 这段代码实现以下需求: 1. 定义一个链表结构体 `List` 和一个节点结构体 `Node`,每个节点包含一个字符串数据和一个指向下一个节点的指针。 2. 读入多个字符串存入链表。 3. 遍历链表,删除包含字符 'z' 或 'm' 的节点。 4. 遍历链表,输出每个节点的字符串数据。 5. 释放链表和节点的内存。 希望这个回答能够帮到你!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

伟子涵

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值