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.生成效果 

 

 

 

 

 

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/460513.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

故障诊断 | 一文解决,TCN时间卷积神经网络模型的故障诊断(Matlab)

效果一览 文章概述 故障诊断 | 一文解决,TCN时间卷积神经网络模型的故障诊断(Matlab) 模型描述 时间卷积神经网络(TCN)是一种用于序列数据建模和预测的深度学习模型。它通过卷积操作在时间维度上对序列数据进行特征提取,并且可以处理可变长度的输入序列。 要使用TCN进行…

L1-080 乘法口诀数列

一、题目 二、解题思路 三、代码 #include<iostream> using namespace std; int main() {int a1,a2,n;cin>>a1>>a2>>n;if(n1){cout<<a1;return 0; }int a[n*2];cout<<a1<<" "<<a2;a[0]a1;a[1]a2;for(int i2,j2;i&l…

05 06 Verilog基础语法与应用讲解

05. 1. 位操作 计数器实验升级&#xff0c;设计8个LED灯以每个0.5s的速率循环闪烁&#xff08;跑马灯&#xff09; 1.1 方法1&#xff1a;使用移位操作符<<来控制led灯的循环亮灭 设计代码 Verilog中&#xff0c;判断操作的时候不加位宽限定是可以的&#xff0c;比如i…

Qt QVariant类应用

QVariant类 QVariant类本质为C联合(Union)数据类型&#xff0c;它可以保存很多Qt类型的值&#xff0c;包括 QBrush&#xff0c;QColor&#xff0c;QString等等&#xff0c;也能存放Qt的容器类型的值。 QVariant::StringList 是 Qt 定义的一个 QVariant::type 枚举类型的变量&…

二分算法--模板及原理总结

二分答案 首先我们看这个图&#xff1a; 我们需要二分的答案就是这个临界点x。 什么情况下可以使用二分呢&#xff1a; 具有单调性&#xff08;单调递增&#xff0c;单调递减&#xff09;&#xff0c;二段性&#xff08;整个区间一分为二&#xff0c;一段区间满足&#xff0c;一…

git flow与分支管理

git flow与分支管理 一、git flow是什么二、分支管理1、主分支Master2、开发分支Develop3、临时性分支功能分支预发布分支修补bug分支 三、分支管理最佳实践1、分支名义规划2、环境与分支3、分支图 四、git flow缺点 一、git flow是什么 Git 作为一个源码管理系统&#xff0c;…

Apktool任意文件写入漏洞分析 CVE-2024-21633

前置知识 在复现该漏洞前&#xff0c;有必要了解Apktool和resources.arsc相关的基础知识&#xff0c;方便理解后续POC的构造。 Apktool是一款流行的开源逆向工程软件&#xff0c;用于反编译和编译Android应用&#xff0c;因此&#xff0c;Apktool被许多其他逆向工程软件集成。…

时间序列之周期性

什么是序列相关&#xff1f; 针对时间序列的趋势和季节性&#xff0c;我们可以很容易地利用“时间相关”的属性进行建模&#xff0c;即直接从时间索引中得出特征。但是有些情况下&#xff0c;一些时间序列只能利用“序列相关”属性&#xff0c;即使用序列的历史值作为特征。如…

父母老了,耳朵听不清怎么办?

你有没有发现&#xff0c;随着年纪的增长&#xff0c;父母的耳朵好像越来越不好使了&#xff1a; 家里的电视声越放越大&#xff1b; 和他们说话常常讲到一半就被打岔&#xff1b; 一件事情要重复说好几遍才能听清&#xff1b; …… 也许父母没意识到问题的严重性&#xff0…

flutter开发实战-可扩展popup弹窗template模版样式

flutter开发实战-可扩展popup弹窗template模版样式 最近在看到一个flutter_beautiful_popup&#xff0c;可以美化弹窗窗口样式。该插件通过一个template模版的类BeautifulPopupTemplate作为抽象的base类。 一、基类BeautifulPopupTemplate 在BeautifulPopupTemplate中&…

C语言:分支与循环

创造不易&#xff0c;友友们给个三连吧&#xff01;&#xff01; C语⾔是结构化的程序设计语⾔&#xff0c;这⾥的结构指的是顺序结构、选择结构、循环结构&#xff0c;C语⾔是能够实 现这三种结构的&#xff0c;其实我们如果仔细分析&#xff0c;我们⽇常所⻅的事情都可以拆分…

单片机——ISP下载、ICP下载、IAP下载

文章目录 ISPICPIAP ISP 在线系统编程&#xff0c;使用引导程序加上外围UART/SPI接口烧录 其本质是将程序的hex文件烧录到板子里的过程 可以使用flymcu这个软件 System memory是STM32在出厂时&#xff0c;由ST在这个区域内部预置了一段BootLoader&#xff0c; 也就是我们常说…