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]; //前移Len
for (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];//为遍历到的索引元素赋值
else
temArray[i] = ArrayBorn[i]; //为遍历到的索引元素赋值
}
return temArray; //返回得到的新数组
}
}
}
2.生成效果