Menu
CoddyTech

移除元素

本课属于 Coddy C# 学习旅程的“逻辑与流程”单元。第 58 课,共 66 课。

如果指定的元素存在,Remove(element) 方法会将其从 HashSet 中移除。如果元素成功移除,此方法会返回 true;如果在集合中找不到该元素,则返回 false。

创建一个空的 HashSet:

HashSet<string> fruits = new HashSet<string>();

向集合中添加一些元素:

fruits.Add("Apple");
fruits.Add("Banana");
fruits.Add("Cherry");

执行上述代码后,集合 fruits 包含:

["Apple", "Banana", "Cherry"]

现在,从集合中移除 "Banana":

bool wasRemoved = fruits.Remove("Banana");

移除后,集合包含:

["Apple", "Cherry"]

wasRemoved 的值会是 true,因为找到了 “Banana” 并将其移除了。

如果我们尝试删除一个不存在的元素:

bool wasRemoved = fruits.Remove("Orange");

集合保持不变,wasRemoved 将为 false。

challenge icon

挑战

简单

创建一个名为 RemoveElement 的方法,它接受两个参数:

  1. 一个字符串集合(set)
  2. 一个要移除的字符串(element)

该方法应尝试从集合中移除给定的元素。如果成功移除该元素,则打印 "Element removed: True",否则打印 "Element removed: False"。然后,在新的一行打印更新后的集合。

亲手试试

using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

public class Program
{
    public static void RemoveElement(HashSet<string> set, string element)
    {
        // 在这里编写你的代码
    }
    
    public static void Main()
    {
        HashSet<string> set = new HashSet<string>();
        
        // 读取第一行,检查是否为 JSON 格式
        string firstLine = Console.ReadLine();
        string elementToRemove;
        
        // 检查输入是否为 JSON 数组格式
        if (firstLine != null && firstLine.StartsWith("[") && firstLine.EndsWith("]"))
        {
            try
            {
                // 提取方括号中的内容
                string arrayContent = firstLine.Substring(1, firstLine.Length - 2);
                
                // 使用正则表达式匹配所有带引号的字符串
                MatchCollection matches = Regex.Matches(arrayContent, @"""([^""]*)""");
                
                foreach (Match match in matches)
                {
                    // 添加捕获的分组(不含引号)
                    if (match.Groups.Count > 1)
                    {
                        set.Add(match.Groups[1].Value.Trim());
                    }
                }
                
                // 读取第二行,获取要移除的元素
                string secondLine = Console.ReadLine();
                
                // 检查元素是否为 JSON 字符串格式
                Match elementMatch = Regex.Match(secondLine, @"""([^""]*)""");
                if (elementMatch.Success && elementMatch.Groups.Count > 1)
                {
                    elementToRemove = elementMatch.Groups[1].Value;
                }
                else
                {
                    elementToRemove = secondLine;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error parsing JSON input: {ex.Message}");
                return;
            }
        }
        else
        {
            // 处理传统输入格式
            string[] elements = firstLine.Split(',');
            foreach (string element in elements)
            {
                set.Add(element.Trim());
            }
            
            // 按传统格式读取要移除的元素
            elementToRemove = Console.ReadLine();
        }
        
        RemoveElement(set, elementToRemove);
    }
}
quiz icon自我测试

这节课包含一个小测验。开始学习即可作答,并记录你的进度。

逻辑与流程中的所有课

自己练习: C# 在线编译器