C#移除List中特定元素
在List里面移除其中一个元素之后,原有的索引以及索引对应的值会发生改变,如果按照原有的索引值删除,就会误删除其它元素。
1.实现思路
原始List为A,将需要删除的元素放到一个List B里面,遍历此List,A再使用Remove依次移除元素。下面代码实现的是移除比新插入的seq小2的元素
2.代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;
namespace PracticeProject
{
internal class Program
{
static void Main(string[] args)
{
ListTest();
Console.ReadLine();
}
public static void ListTest()
{
//定义一个元素为Test的集合tests
List<Test> tests = new List<Test>();
//向List里面添加元素
Test test = new Test();
test.seq = 1; ;
test.content = "第一";
tests.Add(test);
Test test1 = new Test();
test1.seq = 2; ;
test1.content = "第二";
tests.Add(test1);
Test test2 = new Test();
test2.seq = 3; ;
test2.content = "第三";
tests.Add(test2);
Test test3 = new Test();
test3.seq = 4; ;
test3.content = "第四";
tests.Add(test3);
Console.WriteLine("移除之前");
for (int i = 0; i < tests.Count; i++)
{
Console.WriteLine(tests[i].seq + " " + tests[i].content + " 序列号" + i);
}
tests.Remove(tests[1]);
Console.WriteLine("移除索引为1的元素");
for (int i = 0; i < tests.Count; i++)
{
Console.WriteLine(tests[i].seq + " " + tests[i].content + " 序列号" + i);
}
//定义一个元素类型为Test的集合reamoveList,存放需要删除的元素
List<Test> reamoveList = new List<Test>();
int maxValue = tests[tests.Count - 1].seq - 2;
for (int i = 0; i < tests.Count; i++)
{
if (tests[i].seq <= maxValue)
{
reamoveList.Add(tests[i]);
}
}
for (int i = 0; i < reamoveList.Count; i++)
{
//删除tests中的元素
tests.Remove(reamoveList[i]);
}
Console.WriteLine("移除之后");
for (int i = 0; i < tests.Count; i++)
{
Console.WriteLine(tests[i].seq + " " + tests[i].content + " 序列号" + i);
}
}
}
public class Test
{
public int seq { get; set; }
public string content { get; set; }
}
}
3.运行结果

C#移除List中特定元素的更多相关文章
- 三种移除list中的元素(可靠)
/** * 直接使用foreach方法移除list中的元素会抛异常 * Exception in thread "main" java.util.ConcurrentModific ...
- 巧妙利用ToArray()函数移除集合中的元素
当我们对集合foreach遍历时,不能直接移除遍历的集合的元素,解决的方法有很多种,见我之前的随笔: http://www.cnblogs.com/527289276qq/p/4331000.html ...
- LeetCode 27 Remove Element (移除数组中指定元素)
题目链接: https://leetcode.com/problems/remove-element/?tab=Description Problem : 移除数组中给定target的元素,返回剩 ...
- js能力测评——移除数组中的元素
移除数组中的元素 题目描述 : 移除数组 arr 中的所有值与 item 相等的元素.不要直接修改数组 arr,结果返回新的数组 示例1 输入 [1, 2, 3, 4, 2], 2 输出 [1, 3, ...
- LeetCode数组移除数组中目标元素等题目
一种自己解题,一种高赞解题 /** * 移除数组中目标元素,返回新数组长度 * @param nums * @param val * @return */ public int removeEleme ...
- Leetcode27--->Remove Element(移除数组中给定元素)
题目:给定一个数组array和一个值value,移除掉数组中所有与value值相等的元素,返回新的数组的长度:要求:不能分配额外的数组空间,且必须使用原地排序的思想,空间复杂度O(1); 举例: Gi ...
- c#List移除列表中的元素
对于一个List<T>对象来说移除其中的元素是常用的功能.自己总结了一下,列出自己所知的几种方法. class Program { static void Main(string[] ar ...
- js小练习-移除数组中的元素
移除数组 arr 中的所有值与 item 相等的元素,直接在给定的 arr 数组上进行操作,并将结果返回 代码: <!DOCTYPE HTML><html> <he ...
- [google面试CTCI] 2-1.移除链表中重复元素
[链表] Q:Write code to remove duplicates from an unsorted linked list FOLLOW UP How would yo ...
- 移除list中null元素
查询结果为null, list.size()却是1 移除该null元素 totalList.removeAll(Collections.singleton(null));
随机推荐
- springboot多模块打包报错问题根因分析:Unable to find main class
问题背景: 项目结构为springboot多模块,其中有四个模块bean.utils.user.ems,其中user和ems模块为主程序,包含启动类,其他两个模块为其服务,提供依赖 问题分析: 查看u ...
- 书写自动智慧文本分类器的开发与应用:支持多分类、多标签分类、多层级分类和Kmeans聚类
书写自动智慧文本分类器的开发与应用:支持多分类.多标签分类.多层级分类和Kmeans聚类 文本分类器,提供多种文本分类和聚类算法,支持句子和文档级的文本分类任务,支持二分类.多分类.多标签分类.多层级 ...
- 设计模式-1 单例模式 SingletonPattern
23种设计模式 一.创建型 1,AbstractFactory(抽象工厂,对象模式) 2,Builder(建造者,对象模式) 3,Factory Method(工厂方法,类创模式) 4,Prototy ...
- 常见的WCF面试问题
WCF和ASMX WebService的区别是什么? 最基本的区别在于,ASMX或者ASP.NET WebService是用来通过基于HTTP的SOAP来实现通讯.但WCF可以使用任意协议(HTTP, ...
- (C语言)格式输出,右对齐
printf("%8d", i);可以输出整数 i,让它占至少 8 个字符的宽度(即场宽为8):如果 i 不够8 位则在左边补空格使它右对齐满 8 位,如果 i 的输出的位数 ≥ ...
- .NET 云原生架构师训练营(模块二 基础巩固 引入)--学习笔记
2.1 引入 http协议 web server && web application framework .net 与 .net core asp .net core web api ...
- OGG-Postgres实时同步到MySQL
(一)数据库信息 名称 源端数据库 目标端数据库 数据库类型 Postgresql 12.4 MySQL 5.7 IP地址 20.2.127.23 20.2.127.24 端口 5432 3306 数 ...
- Python 装饰器解析(二)
前面一篇文章介绍了python装饰器,最后引入了functools.wraps的使用,本篇文章将对它进行深入的探究. functools模块提供了一系列的高阶函数以及对可调用对象的操作,其中为人熟知的 ...
- IntersectionObserver对象
IntersectionObserver对象 IntersectionObserver对象,从属于Intersection Observer API,提供了一种异步观察目标元素与其祖先元素或顶级文档视 ...
- Mac M1 在PyCharm中安装(支持GPU)TensorFlow 方法
本文介绍在Mac M1的PyCharm中安装TensorFlow与创建工程的方法,在2021的MacBook Pro (M1 Max处理器)验证OK. 安装TensorFlow与创建工程是在Minif ...