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

C#静态数组删除数组元素不改变数组长度 vs 动态数组删除数组元素改变数组长度

目录

一、使用的方法

1.对静态数组删除指定长度并不改变数长度的方法

(1)静态数组

(2)对静态数组删除元素不得改变其长度

2.对动态数组删除指定长度并改变数长度的方法

(1)动态数组

(2)对动态数组删除元素并改变其长度

二、实例1:静态数组并不改变数组长度

1.源码

2.生成效果

三、实例2:动态数组并改变数组长度

1.源码

2.生成效果 


一、使用的方法

1.对静态数组删除指定长度并不改变数长度的方法

(1)静态数组

        静态数组是指数组元素的个数是固定不变的,即它们占用的内存空间大小是固定不变的。

(2)对静态数组删除元素不得改变其长度

        首先需要定义一个一维数组、要删除的开始索引和要删除的长度,然后判断要删除的开始索引和删除的长度是否超出了数组的范围,如果超出,则返回;否则,使用数组中后面的值覆盖前面的值,并将删除长度的数组后面的元素值全部初始化为0,这样就实现了在不改变数组长度的情况下,删除数组中元素的功能。

2.对动态数组删除指定长度并改变数长度的方法

(1)动态数组

        动态数组的声明实际上就是将数组的声明部分和初始化部分分别写在不同的语句中。动态数组的初始化也需要使用new关键字为数组元素分配内存空间,并为数组元素赋初值。

(2)对动态数组删除元素并改变其长度

        首先需要定义一个一维数组、要删除的开始索引和要删除的长度,然后判断要删除的开始索引和删除的长度是否超出了数组的范围。如果删除长度超出了数组范围,则将要删除的元素个数设置为数组的长度;如果删除的开始索引和删除长度超出了数组范围,则将要删除的元素个数设置为数组的长度减去删除开始索引,再减去1;然后定义一个新的数组,其长度设置为原数组长度减去上述运算所得到的长度,最后遍历新数组,并且为新数组的每一个索引赋相应的值即可,这样就实现了删除数组元素后改变其长度的功能。

二、实例1:静态数组并不改变数组长度

1.源码

// 不改变长度删除数组中的元素
namespace _097
{public partial class Form1 : Form{private Button? button1;private TextBox? textBox1;private Label? label1;private Label? label2;private TextBox? textBox2;private TextBox? textBox3;private Button? button2;private Label? label3;private RichTextBox? richTextBox1;private int[] int_array = new int[8];//定义数组类型变量public Form1(){InitializeComponent();StartPosition = FormStartPosition.CenterScreen;Load += Form1_Load;}private void Form1_Load(object? sender, EventArgs e){// // button1// button1 = new Button{Location = new Point(12, 12),Name = "button1",Size = new Size(75, 23),TabIndex = 0,Text = "生成数组",UseVisualStyleBackColor = true};button1.Click += Button1_Click;// // textBox1// textBox1 = new TextBox{Location = new Point(120, 12),Name = "textBox1",Size = new Size(187, 23),TabIndex = 1};// // label1// label1 = new Label{AutoSize = true,Location = new Point(12, 38),Name = "label1",Size = new Size(68, 17),TabIndex = 2,Text = "开始索引:"};// // label2// label2 = new Label{AutoSize = true,Location = new Point(12, 60),Name = "label2",Size = new Size(68, 17),TabIndex = 3,Text = "删除个数:"};// // textBox2// textBox2 = new TextBox{Location = new Point(120, 35),Name = "textBox2",Size = new Size(100, 23),TabIndex = 4};// // textBox3// textBox3 = new TextBox{Location = new Point(120, 60),Name = "textBox3",Size = new Size(100, 23),TabIndex = 5};// // button2// button2 = new Button{Location = new Point(232, 60),Name = "button2",Size = new Size(75, 23),TabIndex = 6,Text = "删除",UseVisualStyleBackColor = true};button2.Click += Button2_Click;// // label3// label3 = new Label{AutoSize = true,Location = new Point(12, 83),Name = "label3",Size = new Size(56, 17),TabIndex = 7,Text = "新数组:"};// // richTextBox1// richTextBox1 = new RichTextBox{Location = new Point(12, 106),Name = "richTextBox1",Size = new Size(295, 43),TabIndex = 8,Text = ""};// // Form1// AutoScaleDimensions = new SizeF(7F, 17F);AutoScaleMode = AutoScaleMode.Font;ClientSize = new Size(319, 161);Controls.Add(richTextBox1);Controls.Add(label3);Controls.Add(button2);Controls.Add(textBox3);Controls.Add(textBox2);Controls.Add(label2);Controls.Add(label1);Controls.Add(textBox1);Controls.Add(button1);Name = "Form1";Text = "不改变长度删除数组中的元素";}/// <summary>/// 生成源数组/// </summary>private void Button1_Click(object? sender, EventArgs e){textBox1!.Clear();for (int i = 0; i < int_array.GetUpperBound(0) + 1; i++){int_array[i] = i;}for (int i = 0; i < int_array.GetUpperBound(0) + 1; i++){textBox1.Text += int_array[i] + " ";}}/// <summary>/// 删除数组元素的事件/// 调用删除方法/// </summary>private void Button2_Click(object? sender, EventArgs e){int index = Convert.ToInt32(textBox2!.Text);int length = Convert.ToInt32(textBox3!.Text);if ((index < 0)||(length < 0)){MessageBox.Show("数组元素索引和删除长度应>0", "提示");return;}else{richTextBox1!.Clear();DeleteArray(int_array, index, length);for (int i = 0; i < int_array.GetUpperBound(0) + 1; i++){richTextBox1.Text += int_array[i] + " ";}}}/// <summary>/// 删除数组中的元素方法/// 如果超出数组范围时,删除长度设=数组长度/// 若索引+长度超出了数组范围,则 Len=数组长度-索引-1/// 后面的元素逐个迁移Len,覆盖删除的,最后补0/// </summary>/// <param name="ArrayBorn">要从中删除元素的数组</param>/// <param name="Index">删除索引</param>/// <param name="Len">删除的长度</param>static void DeleteArray(int[] ArrayBorn, int Index, int Len){if (Index == 0 && Len >= ArrayBorn.Length){Len = ArrayBorn.Length;}if ((Index + Len) >= ArrayBorn.Length){Len = ArrayBorn.Length - Index;}for (int i = 0; i < ArrayBorn.Length - Index - Len; i++)ArrayBorn[i + Index] = ArrayBorn[i + Len + Index];  //前移Lenfor (int j = ArrayBorn.GetUpperBound(0); j > (ArrayBorn.GetUpperBound(0) - Len); j--)//Length-1=索引ArrayBorn[j] = 0;   //补0   }}
}

2.生成效果

三、实例2:动态数组并改变数组长度

1.源码

// 改变长度删除数组中的元素
namespace _098
{public partial class Form1 : Form{private Button? button1;private TextBox? textBox1;private Label? label1;private Label? label2;private TextBox? textBox2;private TextBox? textBox3;private Button? button2;private Label? label3;private RichTextBox? richTextBox1;private readonly int[] int_array = new int[8];//定义数组类型变量public Form1(){InitializeComponent();InitializeComponent();StartPosition = FormStartPosition.CenterScreen;Load += Form1_Load;}private void Form1_Load(object? sender, EventArgs e){// // button1// button1 = new Button{Location = new Point(12, 12),Name = "button1",Size = new Size(75, 23),TabIndex = 0,Text = "生成数组",UseVisualStyleBackColor = true};button1.Click += Button1_Click;// // textBox1// textBox1 = new TextBox{Location = new Point(120, 12),Name = "textBox1",Size = new Size(187, 23),TabIndex = 1};// // label1// label1 = new Label{AutoSize = true,Location = new Point(12, 38),Name = "label1",Size = new Size(68, 17),TabIndex = 2,Text = "开始索引:"};// // label2// label2 = new Label{AutoSize = true,Location = new Point(12, 60),Name = "label2",Size = new Size(68, 17),TabIndex = 3,Text = "删除个数:"};// // textBox2// textBox2 = new TextBox{Location = new Point(120, 35),Name = "textBox2",Size = new Size(100, 23),TabIndex = 4};// // textBox3// textBox3 = new TextBox{Location = new Point(120, 60),Name = "textBox3",Size = new Size(100, 23),TabIndex = 5};// // button2// button2 = new Button{Location = new Point(232, 60),Name = "button2",Size = new Size(75, 23),TabIndex = 6,Text = "删除",UseVisualStyleBackColor = true};button2.Click += Button2_Click;// // label3// label3 = new Label{AutoSize = true,Location = new Point(12, 83),Name = "label3",Size = new Size(56, 17),TabIndex = 7,Text = "新数组:"};// // richTextBox1// richTextBox1 = new RichTextBox{Location = new Point(12, 106),Name = "richTextBox1",Size = new Size(295, 43),TabIndex = 8,Text = ""};// // Form1// AutoScaleDimensions = new SizeF(7F, 17F);AutoScaleMode = AutoScaleMode.Font;ClientSize = new Size(319, 161);Controls.Add(richTextBox1);Controls.Add(label3);Controls.Add(button2);Controls.Add(textBox3);Controls.Add(textBox2);Controls.Add(label2);Controls.Add(label1);Controls.Add(textBox1);Controls.Add(button1);Name = "Form1";Text = "改变长度删除数组中的元素";}/// <summary>/// 生成源数组/// </summary>private void Button1_Click(object? sender, EventArgs e){textBox1!.Clear();for (int i = 0; i < int_array.GetUpperBound(0) + 1; i++){int_array[i] = i;}for (int i = 0; i < int_array.GetUpperBound(0) + 1; i++){textBox1.Text += int_array[i] + " ";}}/// <summary>/// 删除数组元素的事件/// 调用删除方法/// </summary>private void Button2_Click(object? sender, EventArgs e){int index = Convert.ToInt32(textBox2!.Text);int length = Convert.ToInt32(textBox3!.Text);if ((index < 0) || (length < 0)){MessageBox.Show("数组元素索引和删除长度应>0", "提示");return;}else{richTextBox1!.Clear();int[] temArray = DeleteArray(int_array, index, length);for (int i = 0; i < temArray.GetUpperBound(0) + 1; i++){richTextBox1.Text += temArray[i] + " ";}}}/// <summary>/// 删除数组中的元素方法并改变数组长度/// 此方法不改变源数组,即源数组始终存在/// 后面的元素逐个迁移Len,覆盖删除的/// </summary>/// <param name="ArrayBorn">要从中删除元素的数组</param>/// <param name="Index">删除索引</param>/// <param name="Len">删除的长度</param>static int[] DeleteArray(int[] ArrayBorn, int Index, int Len){if (Index == 0 && Len >= ArrayBorn.Length){Len = ArrayBorn.Length;MessageBox.Show("因源数组元素被删光新数组没有元素", "提示");}if ((Index + Len) >= ArrayBorn.Length)//此时,新数组=源数组在索引位置被截尾{Len = ArrayBorn.Length - Index;}int[] temArray = new int[ArrayBorn.Length - Len];   //声明一个新的数组for (int i = 0; i < temArray.Length; i++)           //遍历新数组{if (i >= Index)                                 //判断遍历索引是否大于等于删除索引temArray[i] = ArrayBorn[i + Len];//为遍历到的索引元素赋值elsetemArray[i] = ArrayBorn[i];         //为遍历到的索引元素赋值}return temArray;                            //返回得到的新数组}}
}

2.生成效果 

 

 

 

 

 

 

相关文章:

  • 2024.2.6
  • 艺术创作和生活的关系
  • Docker引擎不同的日志驱动配置以及通过Filebeat采集容器日志的两种解决方案
  • Vue + Element UI el-table + sortablejs 行、列拖拽排序
  • SQL世界之命令语句Ⅲ
  • C语言--------指针(1)
  • [技术杂谈]如何下载vscode历史版本
  • 使用Pillow来生成简单的红包封面
  • freertos 源码分析五 任务调度一
  • 时光峰峦文物璀璨,预防性保护筑安全
  • 鸿蒙4.0.0 安装minitouch
  • 优雅的从HuggingFace下载模型
  • 【算法与数据结构】583、72、LeetCode两个字符串的删除操作+编辑距离
  • Python HTTP隧道在远程通信中的应用:穿越网络的“魔法门”
  • VRRP协议详解
  • es6要点
  • extjs4学习之配置
  • IP路由与转发
  • Java 实战开发之spring、logback配置及chrome开发神器(六)
  • JavaScript 一些 DOM 的知识点
  • Js基础——数据类型之Null和Undefined
  • Python连接Oracle
  • Ruby 2.x 源代码分析:扩展 概述
  • spring + angular 实现导出excel
  • 从重复到重用
  • 得到一个数组中任意X个元素的所有组合 即C(n,m)
  • 记一次用 NodeJs 实现模拟登录的思路
  • 检测对象或数组
  • 前端每日实战 2018 年 7 月份项目汇总(共 29 个项目)
  • 前端每日实战:70# 视频演示如何用纯 CSS 创作一只徘徊的果冻怪兽
  • 如何进阶一名有竞争力的程序员?
  • 原生 js 实现移动端 Touch 滑动反弹
  • 原生js练习题---第五课
  • 宾利慕尚创始人典藏版国内首秀,2025年前实现全系车型电动化 | 2019上海车展 ...
  • ​ssh免密码登录设置及问题总结
  • ​业务双活的数据切换思路设计(下)
  • ()、[]、{}、(())、[[]]命令替换
  • (+4)2.2UML建模图
  • (70min)字节暑假实习二面(已挂)
  • (C语言)编写程序将一个4×4的数组进行顺时针旋转90度后输出。
  • (C语言)二分查找 超详细
  • (六)库存超卖案例实战——使用mysql分布式锁解决“超卖”问题
  • (牛客腾讯思维编程题)编码编码分组打印下标题目分析
  • (一)80c52学习之旅-起始篇
  • (原創) 如何動態建立二維陣列(多維陣列)? (.NET) (C#)
  • (转)ORM
  • ******之网络***——物理***
  • .bat批处理(十一):替换字符串中包含百分号%的子串
  • .NET 8 编写 LiteDB vs SQLite 数据库 CRUD 接口性能测试(准备篇)
  • .NET/C# 使窗口永不获得焦点
  • .NET6 开发一个检查某些状态持续多长时间的类
  • [ web基础篇 ] Burp Suite 爆破 Basic 认证密码
  • [17]JAVAEE-HTTP协议
  • [2018][note]用于超快偏振开关和动态光束分裂的all-optical有源THz超表——
  • [J2ME]url请求返回参数非法(java.lang.illegalArgument)