免费元素素材网站,软件开发者能看到手机信息吗,中文网站建设方案,建设产品网站课程设计目录
一、使用的方法
1.对静态数组删除指定长度并不改变数长度的方法
#xff08;1#xff09;静态数组
#xff08;2#xff09;对静态数组删除元素不得改变其长度
2.对动态数组删除指定长度并改变数长度的方法
#xff08;1#xff09;动态数组
#xff08;21静态数组
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/// 生成源数组/// /summaryprivate 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/// 删除数组元素的事件/// 调用删除方法/// /summaryprivate 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 nameArrayBorn要从中删除元素的数组/param/// param nameIndex删除索引/param/// param nameLen删除的长度/paramstatic 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/// 生成源数组/// /summaryprivate 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/// 删除数组元素的事件/// 调用删除方法/// /summaryprivate 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 nameArrayBorn要从中删除元素的数组/param/// param nameIndex删除索引/param/// param nameLen删除的长度/paramstatic 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.生成效果