C#實(shí)現(xiàn)向數(shù)組指定索引位置插入新的元素值
一、使用的方法
1.自定義插入方法
首先需要定義一個(gè)一維數(shù)組,然后修改數(shù)組的長度(這里使用Length屬性獲取數(shù)組的長度,然后加1,作為新數(shù)組的長度),從而在其中增加一個(gè)元素。只有增加了數(shù)組的長度以后才能在這個(gè)數(shù)組中增加新的元素。
2.使用List<T>.Add(T) 方法
首先,創(chuàng)建一個(gè)與原始數(shù)組大小相同的動(dòng)態(tài)數(shù)組(例如,List<T>)。然后,將原始數(shù)組的元素復(fù)制到動(dòng)態(tài)數(shù)組中,直到到達(dá)要插入元素的索引位置。在動(dòng)態(tài)數(shù)組中插入新元素。將原始數(shù)組中剩余的元素復(fù)制到動(dòng)態(tài)數(shù)組中。最后,將動(dòng)態(tài)數(shù)組轉(zhuǎn)換回?cái)?shù)組并返回。
二、實(shí)例
1.示例1:List<T>.Add(T) 方法
// 將一個(gè)元素插入到現(xiàn)有數(shù)組的指定索引位置,
// 并將原來位于該位置的元素向后移動(dòng)
namespace _095_1
{
class Program
{
static void Main(string[] args)
{
ArgumentNullException.ThrowIfNull(args);
int[] originalArray = [1, 2, 3, 4, 5];
int elementToInsert = 10;
int indexToInsert = 2;
int[] newArray = InsertElement(originalArray, elementToInsert, indexToInsert);
Console.WriteLine(string.Join(", ", newArray));
}
static int[] InsertElement(int[] originalArray, int elementToInsert, int indexToInsert)
{
List<int> dynamicArray = new(originalArray.Length);
// Copy elements from original array to dynamic array until the insertion index
for (int i = 0; i < indexToInsert; i++)
{
dynamicArray.Add(originalArray[i]);
}
// Insert the element at the specified index
dynamicArray.Add(elementToInsert);
// Copy remaining elements from original array to dynamic array
for (int i = indexToInsert; i < originalArray.Length; i++)
{
dynamicArray.Add(originalArray[i]);
}
// Convert dynamic array to a new array and return
return [.. dynamicArray];
}
}
}
//運(yùn)行結(jié)果:
/*
1, 2, 10, 3, 4, 5
*/2.示例:自定義插入方法
//在既有數(shù)組中的指定位置插入一個(gè)新的元素,
//并遍歷輸出新數(shù)組
namespace _095
{
public partial class Form1 : Form
{
private Button? button1;
private Button? button2;
private Label? label1;
private Label? label2;
private TextBox? textBox1;
private TextBox? textBox2;
private RichTextBox? richTextBox1;
private Label? label3;
private TextBox? textBox3;
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, 9),
Name = "button1",
Size = new Size(75, 23),
TabIndex = 0,
Text = "生成數(shù)組",
UseVisualStyleBackColor = true
};
button1.Click += Button1_Click;
//
// button2
//
button2 = new Button
{
Location = new Point(224, 36),
Name = "button2",
Size = new Size(43, 23),
TabIndex = 1,
Text = "確定",
UseVisualStyleBackColor = true
};
button2.Click += Button2_Click;
//
// label1
//
label1 = new Label
{
AutoSize = true,
Location = new Point(12, 42),
Name = "label1",
Size = new Size(56, 17),
TabIndex = 2,
Text = "插入索引"
};
//
// label2
//
label2 = new Label
{
AutoSize = true,
Location = new Point(12, 69),
Name = "label2",
Size = new Size(56, 17),
TabIndex = 3,
Text = "新數(shù)組:"
};
//
// textBox1
//
textBox1 = new TextBox
{
Location = new Point(93, 9),
Name = "textBox1",
Size = new Size(174, 23),
TabIndex = 4
};
//
// textBox2
//
textBox2 = new TextBox
{
Location = new Point(73, 36),
Name = "textBox2",
Size = new Size(40, 23),
TabIndex = 5
};
//
// richTextBox1
//
richTextBox1 = new RichTextBox
{
Location = new Point(12, 90),
Name = "richTextBox1",
Size = new Size(254, 44),
TabIndex = 6,
Text = ""
};
//
// label3
//
label3 = new Label
{
AutoSize = true,
Location = new Point(118, 42),
Name = "label3",
Size = new Size(56, 17),
TabIndex = 7,
Text = "插入元素"
};
//
// textBox3
//
textBox3 = new TextBox
{
Location = new Point(179, 36),
Name = "textBox3",
Size = new Size(40, 23),
TabIndex = 8
};
//
// Form1
//
AutoScaleDimensions = new SizeF(7F, 17F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(279, 146);
Controls.Add(textBox3);
Controls.Add(label3);
Controls.Add(richTextBox1);
Controls.Add(textBox2);
Controls.Add(textBox1);
Controls.Add(label2);
Controls.Add(label1);
Controls.Add(button2);
Controls.Add(button1);
Name = "Form1";
Text = "在數(shù)組中添加一個(gè)元素";
}
/// <summary>
/// 生成數(shù)組事件
/// 遍歷生成整形數(shù)組,并遍歷輸出
/// </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>
/// 確定插入事件
/// 在生成的數(shù)組索引=4的位置插入一個(gè)元素,并遍歷輸出
/// 這個(gè)事件不僅調(diào)用AddArray方法,更是在調(diào)用該方法之后改變了數(shù)組的大小
/// </summary>
private void Button2_Click(object? sender, EventArgs e)
{
richTextBox1!.Clear();
if ((textBox2!.Text != "")&& (textBox3!.Text !="")&& (textBox3!.Text.Length == 1))
{
int_array = AddArray(int_array, Convert.ToInt32(textBox2!.Text), Convert.ToInt32(textBox3!.Text));
for (int i = 0; i < int_array.GetUpperBound(0) + 1; i++)
{
richTextBox1.Text += int_array[i] + " ";
}
}
else
{
MessageBox.Show("輸入信息不能為空且元素長度恒為1", "提示");
}
}
/// <summary>
/// 向數(shù)組中插入單個(gè)元素的方法
/// </summary>
/// <param name="ArrayBorn">要向其中添加元素的一維數(shù)組</param>
/// <param name="Index">添加索引</param>
/// <param name="Value">添加值</param>
/// <returns></returns>
static int[] AddArray(int[] ArrayBorn, int Index, int Value)
{
if (Index >= ArrayBorn.Length)
Index = ArrayBorn.Length - 1;
int[] TemArray = new int[ArrayBorn.Length + 1];//聲明一個(gè)新的數(shù)組
for (int i = 0; i < TemArray.Length; i++)
{
if (Index >= 0)
{
if (i < (Index + 1)) //判斷遍歷到的索引是否小于添加索引加1
TemArray[i] = ArrayBorn[i];
else if (i == (Index + 1))//判斷遍歷到的索引是否等于添加索引加1
TemArray[i] = Value;
else
TemArray[i] = ArrayBorn[i - 1];
}
else
{
if (i == 0)//數(shù)組首位置
TemArray[i] = Value;
else
TemArray[i] = ArrayBorn[i - 1];
}
}
return TemArray;
}
}
}


到此這篇關(guān)于C#實(shí)現(xiàn)向數(shù)組指定索引位置插入新的元素值的文章就介紹到這了,更多相關(guān)C#向數(shù)組插入元素值內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#中的multipart/form-data提交文件和參數(shù)
這篇文章主要介紹了C#中的multipart/form-data提交文件和參數(shù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06
C#結(jié)合JS解決Word添加無效位圖導(dǎo)致進(jìn)程停滯的問題
這篇文章主要為大家詳細(xì)介紹了C#如何結(jié)合JS解決Word添加無效位圖導(dǎo)致進(jìn)程停滯的問題,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-11-11
C#利用反射實(shí)現(xiàn)多數(shù)據(jù)庫訪問
本文詳細(xì)講解了C#利用反射實(shí)現(xiàn)多數(shù)據(jù)庫訪問的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-03-03
c#使用EPPlus將圖片流嵌入到Excel實(shí)現(xiàn)示例
這篇文章主要為大家介紹了c#使用EPPlus將圖片流嵌入到Excel實(shí)現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12
C#模擬Http與Https請(qǐng)求框架類實(shí)例
這篇文章主要介紹了C#模擬Http與Https請(qǐng)求框架類,實(shí)例分析了處理http與https請(qǐng)求的方法與信息處理的技巧,需要的朋友可以參考下2014-12-12

