当前位置: 首页 > news >正文

List<T>属性和方法使用

//@author:shark_ddd
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;//使用函数来减少长度namespace List_T
{class Student{public string Name { get; set; }public int Age { get; set; }}internal class Program{static void PrintStudents(List<Student> students){foreach (Student student in students){Console.Write("Name:" + student.Name);Console.WriteLine(", Age:" + student.Age);}}static void PrintNumbers(List<int> numbers){foreach (int number in numbers){Console.Write(" " + number);}Console.WriteLine();}static void PrintStrings(List<string> strings){foreach (string str in strings){Console.Write(" " + str);}Console.WriteLine();}static void Main(string[] args){List<Student> studentList = new List<Student>{new Student { Name = "Alice", Age = 20 },new Student { Name = "Ben", Age = 18 },new Student { Name = "Clark", Age = 18 }};Console.WriteLine("Students:");PrintStudents(studentList);Console.WriteLine("=================================================================================");// 创建数字列表List<int> numberList = new List<int> { 1, 2, 3, 4, 5 };// 在数字列表末尾加入 6numberList.Add(6);// 在数字列表末尾加入 7、8、9numberList.AddRange(new List<int> { 7, 8, 9 });Console.WriteLine("now_List:");PrintNumbers(numberList);// 获取数字列表中的所有偶数List<int> evenNumbers = numberList.FindAll(n => n % 2 == 0);Console.WriteLine("获取的偶数有:");PrintNumbers(evenNumbers);// 删除数字列表中的 9numberList.Remove(9);Console.WriteLine("now_List:");PrintNumbers(numberList);// 删除数字列表中索引为 0 的元素numberList.RemoveAt(0);Console.WriteLine("now_List:");PrintNumbers(numberList);// 打印第一个Console.WriteLine("First_number:" + numberList.First());Console.WriteLine("=================================================================================");// 水果列表List<string> fruitList = new List<string> { "apple", "banana", "cherry" };// 在索引为 1 的位置插入 orangefruitList.Insert(1, "orange");Console.WriteLine("now_Fruit:");PrintStrings(fruitList);// 复制水果列表从索引 0 开始的两个元素到 myFruit 数组中,并从数组的索引 0 开始存入string[] myFruit = new string[2];fruitList.CopyTo(0, myFruit, 0, 2);Console.WriteLine("myFruit:");Console.WriteLine(string.Join(" ", myFruit));// 复制水果列表从索引 1 开始的三个元素到 subList 中List<string> subList = fruitList.GetRange(1, 3);Console.WriteLine("subList:");PrintStrings(subList);// 翻转水果列表元素排序顺序fruitList.Reverse();Console.WriteLine("now_Fruit:");PrintStrings(fruitList);// 对水果列表元素进行排序fruitList.Sort();Console.WriteLine("now_Fruit:");PrintStrings(fruitList);// 清空水果列表fruitList.Clear();Console.WriteLine("now_Fruit:");PrintStrings(fruitList);Console.ReadLine();}}
}//转换成字符串string.Join
//using System;
//using System.Collections.Generic;
//using System.Linq;
//using System.Text;
//using System.Threading.Tasks;//namespace List_T
//{
//    class Student
//    {
//        public string Name { get; set; }
//        public int Age { get; set; }
//    }//    internal class Program
//    {
//        static void Main(string[] args)
//        {
//            List<Student> studentList = new List<Student>
//            {
//                new Student { Name = "Alice", Age = 20 },
//                new Student { Name = "Ben", Age = 18 },
//                new Student { Name = "Clark", Age = 18 }
//            };//            Console.WriteLine("Students: " + string.Join(", ", studentList.Select(s => $"Name:{s.Name}, Age:{s.Age}")));//            Console.WriteLine("=================================================================================");//            // 创建数字列表
//            List<int> numberList = new List<int> { 1, 2, 3, 4, 5 };//            // 在数字列表末尾加入 6
//            numberList.Add(6);//            // 在数字列表末尾加入 7、8、9
//            numberList.AddRange(new List<int> { 7, 8, 9 });//            Console.WriteLine("now_List: " + string.Join(" ", numberList));//            // 获取数字列表中的所有偶数
//            List<int> evenNumbers = numberList.FindAll(n => n % 2 == 0);
//            Console.WriteLine("获取的偶数有: " + string.Join(" ", evenNumbers));//            // 删除数字列表中的 9
//            numberList.Remove(9);
//            Console.WriteLine("now_List: " + string.Join(" ", numberList));//            // 删除数字列表中索引为 0 的元素
//            numberList.RemoveAt(0);
//            Console.WriteLine("now_List: " + string.Join(" ", numberList));//            // 打印第一个
//            Console.WriteLine("First_number:" + numberList.First());//            Console.WriteLine("=================================================================================");//            // 水果列表
//            List<string> fruitList = new List<string> { "apple", "banana", "cherry" };//            // 在索引为 1 的位置插入 orange
//            fruitList.Insert(1, "orange");
//            Console.WriteLine("now_Fruit: " + string.Join(" ", fruitList));//            // 复制水果列表从索引 0 开始的两个元素到 myFruit 数组中,并从数组的索引 0 开始存入
//            string[] myFruit = new string[2];
//            fruitList.CopyTo(0, myFruit, 0, 2);
//            Console.WriteLine("myFruit: " + string.Join(" ", myFruit));//            // 复制水果列表从索引 1 开始的三个元素到 subList 中
//            List<string> subList = fruitList.GetRange(1, 3);
//            Console.WriteLine("subList: " + string.Join(" ", subList));//            // 翻转水果列表元素排序顺序
//            fruitList.Reverse();
//            Console.WriteLine("now_Fruit: " + string.Join(" ", fruitList));//            // 对水果列表元素进行排序
//            fruitList.Sort();
//            Console.WriteLine("now_Fruit: " + string.Join(" ", fruitList));//            // 清空水果列表
//            fruitList.Clear();
//            Console.WriteLine("now_Fruit: " + string.Join(" ", fruitList));//            Console.ReadLine();
//        }
//    }
//}    /*冗长的foreach
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace List_T
{class Student{public string Name { get; set; }public int Age { get; set; }}internal class Program{static void Main(string[] args){List<Student> studentList = new List<Student>{new Student { Name = "Alice", Age = 20 },new Student { Name = "Ben", Age = 18 },new Student { Name = "Clark", Age = 18 }};foreach (Student student in studentList){Console.Write("Name:" + student.Name);Console.WriteLine(",  Age:" + student.Age);}Console.WriteLine("=================================================================================");// 创建数字列表List<int> numberList = new List<int> { 1, 2, 3, 4, 5 };// 在数字列表末尾加入 6numberList.Add(6);// 在数字列表末尾加入 7、8、9numberList.AddRange(new List<int> { 7, 8, 9 });Console.Write("now_List:");foreach (int number in numberList){Console.Write(" " + number);}Console.WriteLine();// 获取数字列表中的所有偶数List<int> evenNumbers = numberList.FindAll(n => n % 2 == 0);Console.Write("获取的偶数有:");foreach (int number in evenNumbers){Console.Write(" " + number);}Console.WriteLine();// 删除数字列表中的 9numberList.Remove(9);Console.Write("now_List:");foreach (int number in numberList){Console.Write(" " + number);}Console.WriteLine();// 删除数字列表中索引为 0 的元素numberList.RemoveAt(0);Console.Write("now_List:");foreach (int number in numberList){Console.Write(" " + number);}Console.WriteLine();//打印第一个Console.WriteLine("First_number:" + numberList.First());Console.WriteLine("=================================================================================");// 水果列表List<string> fruitList = new List<string> { "apple", "banana", "cherry" };// 在索引为 1 的位置插入 orangefruitList.Insert(1, "orange");Console.Write("now_Fruit:");foreach (string number in fruitList){Console.Write(" " + number);}Console.WriteLine();// 复制水果列表从索引 0 开始的两个元素到 myFruit 数组中,并从数组的索引 0 开始存入string[] myFruit = new string[2];fruitList.CopyTo(0, myFruit, 0, 2);Console.Write("myFruit:");foreach (string number in myFruit){Console.Write(" " + number);}Console.WriteLine();// 复制水果列表从索引 1 开始的三个元素到 subList 中List<string> subList = fruitList.GetRange(1, 3);Console.Write("subList:");foreach (string number in subList){Console.Write(" " + number);}Console.WriteLine();// 翻转水果列表元素排序顺序fruitList.Reverse();Console.Write("now_Fruit:");foreach (string number in fruitList){Console.Write(" " + number);}Console.WriteLine();// 对水果列表元素进行排序fruitList.Sort();Console.Write("now_Fruit:");foreach (string number in fruitList){Console.Write(" " + number);}Console.WriteLine();// 清空水果列表fruitList.Clear();Console.Write("now_Fruit:");foreach (string number in fruitList){Console.Write(" " + number);}Console.WriteLine();Console.ReadLine();}}
}*/

相关文章:

List<T>属性和方法使用

//author&#xff1a;shark_ddd using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;//使用函数来减少长度namespace List_T {class Student{public string Name { get; set; }public int Age { get; set; …...

记一次:使用使用Dbeaver连接Clickhouse

前言&#xff1a;使用了navicat连接了clickhouse我感觉不太好用&#xff0c;就整理了一下dbeaver连接 0、使用Navicat连接clickhouse 测试连接 但是不能双击打开&#xff0c;可是使用命令页界面&#xff0c;右键命令页界面&#xff0c;然后可以用sql去测试 但是不太好用&#…...

Java面向对象编程进阶(四)

Java面向对象编程进阶&#xff08;四&#xff09; 一、equals()方法的使用二、toString()方法的使用三、复习 一、equals()方法的使用 适用性&#xff1a;任何引用数据都可以使用。 自定义的类在没有重写Object中equals()方法的情况下&#xff0c;调用的就是Object类中声明的…...

【51单片机】第一个小程序 —— 点亮LED灯

学习使用的开发板&#xff1a;STC89C52RC/LE52RC 编程软件&#xff1a;Keil5 烧录软件&#xff1a;stc-isp 开发板实图&#xff1a; 文章目录 单片机介绍LED灯介绍练习创建第一个项目点亮LED灯LED周期闪烁 单片机介绍 单片机&#xff0c;英文Micro Controller Unit&#xff0…...

如何通过自动化有效地简化 Active Directory 操作?

我们都知道规模稍微大一点的企业为了便于计算机的管理&#xff0c;基本都上了微软的AD域控制器。 那么肯定就会存在这么一个问题&#xff0c; 不断的会有计算机加入或者是退出域控制器&#xff0c;批量的创建、修改、删除AD域用户&#xff0c;如果企业的架构需要改变&#xff…...

Java-POI导出EXCEL(动态表头)

1、主要功能 导出excel&#xff0c;表头有固定的和动态的。动态表头之间不能穿插固定表头。 2、使用方法 引入下方两个工具类&#xff0c;定义excel固定表头类。调用方法即可。 调用方法&#xff1a; ExcelDynamicHeader<MajorNameChangeReport> ledgerSafetyProblemEx…...

利用 Direct3D 绘制几何体—9.流水线状态对象

到目前为止展示过编写输入布局描述、创建顶点着色器和像素着色器&#xff0c;以及配置光栅器状态组这 3 个步骤。接下来讲如何将这些对象绑定到图形流水线上&#xff0c;用以实际绘制图形。大多数控制图形流水线状态的对象被统称为流水线状态对象&#xff08;Pipeline State Ob…...

【开源项目】libfaketime安装、使用——小白教程

项目 Github&#xff1a;GitHub - wolfcw/libfaketime: libfaketime modifies the system time for a single application libfaketime安装 01.切换路径&#xff0c;目标路径&#xff1a;/usr/local &#xff08;在/usr/local路径下git clone 开源项目) 切换路径指令: cd …...

java.util.concurrent包

java.util.concurrent包是Java中用于并发编程的重要工具集&#xff0c;提供了丰富的并发原语和组件&#xff0c;以简化多线程编程的复杂性&#xff0c;并帮助开发者编写高效、可伸缩和线程安全的并发程序。其主要功能包括以下几个方面&#xff1a; 一、线程池和任务执行框架 …...

Django创建项目模块+创建映射类+视图

确保你的项目已经正确链接数据库 链接数据库的工具有很多,数据库的种类也有很多&#xff0c;我使用的数据库是mysql&#xff0c;工具是pmysql&#xff0c;使用pymysql链接数据库&#xff0c;在settings文件中这么设置&#xff1a; DATABASES {# default: {# ENGINE: dja…...

使用AMD GPU和LangChain构建问答聊天机器人

Question-answering Chatbot with LangChain on an AMD GPU — ROCm Blogs 作者&#xff1a;Phillip Dang 2024年3月11日 LangChain是一个旨在利用语言模型强大功能来构建前沿应用程序的框架。通过将语言模型连接到各种上下文资源并基于给定的上下文提供推理能力&#xff0c;L…...

2024年808数据结构答案

1.已知带头结点单链表&#xff0c;H为头指针。设计一个算法&#xff0c;查找到链表的第m个结点(不包含头结点)&#xff0c;并将元 素值为X的结点插入到该结点后&#xff0c;形成一个新的链表。 // 定义单链表节点结构 typedef struct Node {int data;struct Node* next; } Nod…...

Amazon Linux 2023 安装 Docker

Amazon Linux 2023 安装 Docker 1. 简介 在公司需要将代码部属到 Amazon Linux 2023 系统上时&#xff0c;去 Docker 官方文档里面看也没有针对该系统的部属文档。虽然有通用的 Linux 部属方案但不能应用包管理工具。 首先执行yum、dnf、apt&#xff0c;执行yum和dnf都有正确…...

接口测试(八)jmeter——参数化(CSV Data Set Config)

一、CSV Data Set Config 需求&#xff1a;批量注册5个用户&#xff0c;从CSV文件导入用户数据 1. 【线程组】–>【添加】–>【配置元件】–>【CSV Data Set Config】 2. 【CSV数据文件设置】设置如下 3. 设置线程数为5 4. 运行后查看响应结果...

GGD证明推导学习

GGD证明推导学习 这篇文章&#xff0c;建议先看相关的论文。这篇是我读证明的感悟&#xff0c;因此&#xff0c;不会论文的主体内容 首先&#xff0c;给出命题&#xff1a; DGI的sumary向量是一个常数 给定一个图&#xff1a; G { X ∈ R N D , A ∈ R N N } \mathcal{G…...

Flink难点和高频考点:Flink的反压产生原因、排查思路、优化措施和监控方法

目录 反压定义 反压影响 WebUI监控 Metrics指标 backPressureTimeMsPerSecond idleTimeMsPerSecond busyTimeMsPerSecond 反压可视化 资源优化 算子优化 数据倾斜优化 复杂算子优化 背压机制 反压预防 性能调优 内置工具 第三方工具 反压定义 在探讨Flink的性…...

Swarm - Agent 编排工具

文章目录 一、关于 Swarm&#xff08;实验性、教育性&#xff09;为什么选择蜂群文档 二、安装使用安装基本用法其它示例 三、Running Swarmclient.run()ArgumentsResponse字段 四、AgentFields Agent指令函数切换和更新上下文变量函数模式 流媒体评估工具 一、关于 Swarm&…...

使用Python中的jieba库进行简单情感分析

在自然语言处理&#xff08;NLP&#xff09;领域&#xff0c;情感分析是一项重要的任务&#xff0c;它可以帮助我们理解文本背后的情感倾向。本文将通过一个简单的例子来介绍如何使用Python的jieba库对中文文本进行基本的情感分析。 1. 环境准备 首先&#xff0c;确保已经安装…...

`pip` 下载速度慢

pip 下载速度慢&#xff08;例如只有 50KB/s&#xff09;可能由多个因素导致&#xff0c;以下是一些常见原因和解决方法&#xff1a; 1. 使用国内镜像源 国内访问 PyPI 服务器可能会较慢&#xff0c;您可以通过配置国内镜像源来提升下载速度。以下是一些常用的国内镜像源&…...

【WRF数据准备】基于GEE下载静态地理数据-叶面积指数LAI及绿色植被率Fpar

【WRF数据准备】基于GEE下载静态地理数据 准备:WRF所需静态地理数据(Static geographical data)数据范围说明基于GEE下载叶面积指数及绿色植被率GEE数据集介绍数据下载:LAI(叶面积指数)和Fpar(绿色植被率)数据处理:基于Python处理为单波段LAI数据参考GEE的介绍可参见另…...

浅谈 React Hooks

React Hooks 是 React 16.8 引入的一组 API&#xff0c;用于在函数组件中使用 state 和其他 React 特性&#xff08;例如生命周期方法、context 等&#xff09;。Hooks 通过简洁的函数接口&#xff0c;解决了状态与 UI 的高度解耦&#xff0c;通过函数式编程范式实现更灵活 Rea…...

web vue 项目 Docker化部署

Web 项目 Docker 化部署详细教程 目录 Web 项目 Docker 化部署概述Dockerfile 详解 构建阶段生产阶段 构建和运行 Docker 镜像 1. Web 项目 Docker 化部署概述 Docker 化部署的主要步骤分为以下几个阶段&#xff1a; 构建阶段&#xff08;Build Stage&#xff09;&#xff1a…...

云原生核心技术 (7/12): K8s 核心概念白话解读(上):Pod 和 Deployment 究竟是什么?

大家好&#xff0c;欢迎来到《云原生核心技术》系列的第七篇&#xff01; 在上一篇&#xff0c;我们成功地使用 Minikube 或 kind 在自己的电脑上搭建起了一个迷你但功能完备的 Kubernetes 集群。现在&#xff0c;我们就像一个拥有了一块崭新数字土地的农场主&#xff0c;是时…...

Java 语言特性(面试系列1)

一、面向对象编程 1. 封装&#xff08;Encapsulation&#xff09; 定义&#xff1a;将数据&#xff08;属性&#xff09;和操作数据的方法绑定在一起&#xff0c;通过访问控制符&#xff08;private、protected、public&#xff09;隐藏内部实现细节。示例&#xff1a; public …...

LeetCode - 394. 字符串解码

题目 394. 字符串解码 - 力扣&#xff08;LeetCode&#xff09; 思路 使用两个栈&#xff1a;一个存储重复次数&#xff0c;一个存储字符串 遍历输入字符串&#xff1a; 数字处理&#xff1a;遇到数字时&#xff0c;累积计算重复次数左括号处理&#xff1a;保存当前状态&a…...

系统设计 --- MongoDB亿级数据查询优化策略

系统设计 --- MongoDB亿级数据查询分表策略 背景Solution --- 分表 背景 使用audit log实现Audi Trail功能 Audit Trail范围: 六个月数据量: 每秒5-7条audi log&#xff0c;共计7千万 – 1亿条数据需要实现全文检索按照时间倒序因为license问题&#xff0c;不能使用ELK只能使用…...

STM32F4基本定时器使用和原理详解

STM32F4基本定时器使用和原理详解 前言如何确定定时器挂载在哪条时钟线上配置及使用方法参数配置PrescalerCounter ModeCounter Periodauto-reload preloadTrigger Event Selection 中断配置生成的代码及使用方法初始化代码基本定时器触发DCA或者ADC的代码讲解中断代码定时启动…...

DBAPI如何优雅的获取单条数据

API如何优雅的获取单条数据 案例一 对于查询类API&#xff0c;查询的是单条数据&#xff0c;比如根据主键ID查询用户信息&#xff0c;sql如下&#xff1a; select id, name, age from user where id #{id}API默认返回的数据格式是多条的&#xff0c;如下&#xff1a; {&qu…...

AI编程--插件对比分析:CodeRider、GitHub Copilot及其他

AI编程插件对比分析&#xff1a;CodeRider、GitHub Copilot及其他 随着人工智能技术的快速发展&#xff0c;AI编程插件已成为提升开发者生产力的重要工具。CodeRider和GitHub Copilot作为市场上的领先者&#xff0c;分别以其独特的特性和生态系统吸引了大量开发者。本文将从功…...

【HTTP三个基础问题】

面试官您好&#xff01;HTTP是超文本传输协议&#xff0c;是互联网上客户端和服务器之间传输超文本数据&#xff08;比如文字、图片、音频、视频等&#xff09;的核心协议&#xff0c;当前互联网应用最广泛的版本是HTTP1.1&#xff0c;它基于经典的C/S模型&#xff0c;也就是客…...